Spring Boot JSR303 message code in annotation getting ignored

前端 未结 2 882
生来不讨喜
生来不讨喜 2020-12-18 00:39

In my Spring Boot app I have a backing bean where I am using JSR303 validation. In the annotation, I have specified the message code:

@NotBlank(message = \"         


        
相关标签:
2条回答
  • 2020-12-18 01:18

    JSR303 interpolation normally works with ValidationMessages.properties file. However you can configure Spring to change that if you want (I was lazy to do so :)) e.g.

    <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
        <property name="validationMessageSource" ref="messageSource" />
    </bean>
    
    <mvc:annotation-driven validator="validator" />
    
    0 讨论(0)
  • 2020-12-18 01:28

    According to JSR-303 specification message parameters are expected to be stored in ValidationMessages.properties files. But you can override the place where they are looked for.

    So you have 2 options:

    1. Move your messages to the ValidationMessages.properties file
    2. Or override getValidator() method of your WebMvcConfigurerAdapter's descendant (JavaConfig in your case):

      @Override
      public Validator getValidator() {
          LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
          validator.setValidationMessageSource(messageSource());
          return validator;
      }
      
    0 讨论(0)
提交回复
热议问题