Springboot错误消息插值不适用于自定义验证器 [英] Springboot error message interpolation not working for custom validator

查看:183
本文介绍了Springboot错误消息插值不适用于自定义验证器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我混合了 org.springframework.validation 和JSR-303注释。

I mixed both org.springframework.validation together with JSR-303 annotation.

JSR-303注释:

JSR-303 annotation:

    public class Model{

        private String type;

        private State state;

        @NotNull(message = "{comment.notnull}")
        private String comment;
    }

Spring框架验证:

Spring framework validation:

@Component
public class ModelValidator implements Validator {
    @Override
    public boolean supports(Class<?> clazz) {
        return Model.class.equals(clazz);
    }

    @Override
    public void validate(Object obj, Errors errors) {
        Model model = (Model) obj;

        if (eventModel.getState() == null) {
            errors.reject("state", "error.state.invalidState");
        }
    }
}

我的 ValidationMessages_en.properties

error.state.invalidState=Invalid state.
comment.notnull=Comment not null.

然后我的配置:

@Configuration
public class ValidationConfig {

    @Bean
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("ValidationMessages");
        return messageSource;
    }
}

当我运行springboot应用程序时,插值适用于JSR-303,但不是自定义验证器,我错过了什么?试了好久但却想不通。

When i run my springboot application, the interpolation works for JSR-303, but not the custom validator, did i miss something? Tried for long time but can't figure out.

结果:

"error_messages": [
    {
        "error_message": "Comment not null"
    },
    {
        "error_message": "error.state.invalidState"
    }
]


推荐答案

我不确定是否有任何简单的方法,但最终我必须这样做,

I am not sure if there is any easy way but eventually I had to do like this ,

添加以下组件,

import java.util.Locale;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.stereotype.Component;

@Component
public class Messages {
    @Autowired
    private MessageSource messageSource;

    private MessageSourceAccessor accessor;

    @PostConstruct
    private void init() {
    accessor = new MessageSourceAccessor(messageSource, Locale.ENGLISH);
    }

    public String get(String property) {
    return accessor.getMessage(property);
    }

}

然后我会注入这个组件我的验证员&使用上面 get(error.state.invalidState)此类的方法而不是 error.state.invalidState 直接。

Then I would inject this component in my validator & use above get("error.state.invalidState") method of this class instead of error.state.invalidState directly.

messageSource 是您已在系统中定义的bean。可以外部化区域设置,并可以使用其他配置设置默认区域设置。

messageSource is the bean that you already defined in system. Locale can be externalized and default locale can be set with another configuration.

MessageSourceAccessor 有很多重载方法,如果希望提供这些选项,你也可以直接公开它。

MessageSourceAccessor has lots of overloaded methods, you can directly expose that too if wish to provide those options.

这篇关于Springboot错误消息插值不适用于自定义验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆