Spring Bootvalidation消息未解析

我无法解析validation消息。

我一直在搜索和阅读网络和SO几个小时,我想将问题与自定义弹簧validation错误的标记答案联系起来

我确实定义了一个MessageSource bean,并且正确读取了messages.properties ,因为我还使用它来显示常规文本th:text="#{some.prop.name} ,它确实可以正常工作。只是validation错误将无法正常工作。我确定这是一个我忽略的愚蠢错误……validation本身工作正常。

约束:

 @NotEmpty(message="{validation.mail.notEmpty}") @Email() private String mail; 

messages.properties:

 # Validation validation.mail.notEmpty=The mail must not be empty! 

模板部分:

  

显示的文字:

 {validation.mail.notEmpty} 

我尝试了很多变化,都没有成功。

 @NotEmpty(message="validation.mail.notEmpty") @NotEmpty(message="#{validation.mail.notEmpty}") 

只显示消息字符串的确切值,不解析。

     

会导致错误。


编辑:

经过调试,我偶然发现了这个问题:

类: org.springframework.context.support.MessageSourceSupport

方法: formatMessage(String msg, Object[] args, Locale locale)

将被召唤

formatMessage("{validation.mail.notEmpty}", null, locale /*German Locale*/)

它会遇到if (messageFormat == INVALID_MESSAGE_FORMAT) {

所以…我的消息格式不正确。 这超出了我的范围/知识。 谁知道这意味着什么?

看起来您在应用程序配置中缺少LocalValidatorFactoryBean定义。 下面您可以找到定义两个bean的Application类示例: LocalValidatorFactoryBean和使用messages.properties文件的MessageSource

 import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.MessageSource; import org.springframework.context.annotation.Bean; import org.springframework.context.support.ReloadableResourceBundleMessageSource; import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; @SpringBootApplication public class Application { @Bean public MessageSource messageSource() { ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource(); messageSource.setBasename("classpath:messages"); messageSource.setDefaultEncoding("UTF-8"); return messageSource; } @Bean public LocalValidatorFactoryBean validator() { LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean(); bean.setValidationMessageSource(messageSource()); return bean; } public static void main(String[] args) { SpringApplication.run(Application.class, args); } } 

定义了LocalValidatorFactoryBean bean后,您可以使用自定义validation消息,如:

 @NotEmpty(message = "{validation.mail.notEmpty}") @Email private String email; 

messages.properties

 validation.mail.notEmpty=E-mail cannot be empty! 

和Thymeleaf模板文件:

 

Name Error

样品申请

https://github.com/wololock/stackoverflow-answers/tree/master/45692179

我准备了示例Spring Boot应用程序来反映您的问题。 随意克隆它并在本地运行它。 如果使用表单发布的值不符合@NotEmpty@Emailvalidation,它将显示已翻译的validation消息。

WebMvcConfigurerAdapter配置

在扩展WebMvcConfigurerAdapter情况下,您必须通过从父类重写getValidator()方法来提供validation器,例如:

 import org.springframework.context.MessageSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.support.ReloadableResourceBundleMessageSource; import org.springframework.validation.Validator; import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration @EnableWebMvc public class WebConfiguration extends WebMvcConfigurerAdapter { @Bean public MessageSource messageSource() { ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource(); messageSource.setBasename("classpath:messages"); messageSource.setDefaultEncoding("UTF-8"); return messageSource; } @Bean @Override public Validator getValidator() { LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean(); bean.setValidationMessageSource(messageSource()); return bean; } // other methods... } 

否则,如果您在其他位置定义LocalValidatorFactoryBean bean,它将被覆盖并且不会产生任何影响。

我希望它有所帮助。

不确定您使用的是哪个版本的弹簧靴。 我正在使用Spring boot 2.0.1.RELEASE 。 更清晰的解决方案是将所有validation消息移至ValidationMessages.properties 。 这样您就不必覆盖自动配置的Validator()并设置MessageSource