【问题标题】:Springboot error message interpolation not working for custom validatorSpringboot 错误消息插值不适用于自定义验证器
【发布时间】:2018-12-06 10:30:43
【问题描述】:

我将org.springframework.validation 与 JSR-303 注释混合在一起。

JSR-303 注释:

    public class Model{

        private String type;

        private State state;

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

Spring 框架验证:

@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,但不适用于自定义验证器,我错过了什么吗?试了很久还是搞不定。

结果:

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

【问题讨论】:

    标签: spring-boot java-8 bean-validation


    【解决方案1】:

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

    添加到组件下方,

    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

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

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

    【讨论】:

    • 是的,你的方法确实有效,我会等待一段时间看看是否有更好的答案,如果没有,我会接受你的。谢谢!!
    【解决方案2】:

    我认为你混淆了两种方法:

    reject(String errorCode, String defaultMessage)

    rejectValue(String field, String errorCode)

    由于您的配置看起来不错,只需执行以下操作即可解决您的问题:

    errors.rejectValue("state", "error.state.invalidState");
    

    或者

    errors.reject("error.state.invalidState", "Some default message to fallback!");
    

    【讨论】:

      猜你喜欢
      • 2017-09-14
      • 2011-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 2016-04-18
      相关资源
      最近更新 更多