spring-boot icon indicating copy to clipboard operation
spring-boot copied to clipboard

Bean validation does not use defined message-soure

Open yuvalbo opened this issue 9 years ago • 4 comments

I have this setup (Working with 2.0.0.BUILD-SNAPSHOT)

Gradle snippet:

    compile('org.springframework.boot:spring-boot-devtools')
    compile('org.springframework.security.oauth:spring-security-oauth2');
    compile group: 'com.zaxxer', name: 'HikariCP', version: '2.5.1'
    compile 'org.apache.commons:commons-lang3:3.5'
    compile group: 'org.aspectj', name: 'aspectjweaver', version: '1.8.9'

    compile group: 'io.katharsis', name: 'katharsis-spring', version: '2.8.2'
    compile group: 'io.katharsis', name: 'katharsis-core', version: '2.8.2'
    compile group: 'io.katharsis', name: 'katharsis-rs', version: '2.8.2'


//    compile 'org.reflections:reflections:0.9.10'
    //Base Setup
    compile('org.springframework.boot:spring-boot-starter-actuator')
    compile('org.springframework.boot:spring-boot-starter-aop')
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-security')
    compile('org.springframework.boot:spring-boot-starter-thymeleaf')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework.boot:spring-boot-starter-validation')
    compile('org.springframework.boot:spring-boot-starter-websocket')
    runtime('mysql:mysql-connector-java')
    testCompile( group: 'junit', name: 'junit', version: '4.12')
@Configuration
public class I18NConfiguration {
    @Value("${xyz.messages.base}")
    private String messagesBase;


    @Bean(name = "messageSource")
    public ReloadableResourceBundleMessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasenames(messagesBase);
        messageSource.setDefaultEncoding("UTF-8");
        return messageSource;
    }


    @Bean
    public LocalValidatorFactoryBean validator() {
        final LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean();
        localValidatorFactoryBean.setValidationMessageSource(messageSource());
        return localValidatorFactoryBean;
    }

My application.yml contains:

xyz:
  messages:
    base: file:config/i18n/messages

I have even added (Although doesn't seems that its needed):

@Configuration
@EnableWebMvc
public class WebConfiguration extends WebMvcConfigurerAdapter {

    private Validator validator;

    WebConfiguration(Validator validator){
        this.validator = validator;
    }

    @Override
    public Validator getValidator() {
        return validator;
    }

}

But when resolving errors (e.g. org.hibernate.validator.constraints.Length.message) messages are taken from the default hibernate-validator-5.2.4.Final.jar!/org/hibernate/validator/ValidationMessages.properties instead of my custom messages.properties.

yuvalbo avatar Dec 24 '16 16:12 yuvalbo

Rather than paste code snippets into the issue, are you able to provide a sample application on GitHub that we can clone and run? Thanks.

philwebb avatar Dec 27 '16 19:12 philwebb

Hi @philwebb not sure it is a legit request to create a project for introducing a bug/issue, but maybe I am wrong here, don't have the time for it. Anyway I solved the problem so you might want to use the solution or better yet provide a built in support/solution for it:

I defined the following:

@Configuration
public class HibernateValidationConfiguration extends HibernateJpaAutoConfiguration {

    @Autowired
    private Validator validator;

    public HibernateValidationConfiguration(DataSource dataSource, JpaProperties jpaProperties, ObjectProvider<JtaTransactionManager> jtaTransactionManagerProvider) {
        super(dataSource, jpaProperties, jtaTransactionManagerProvider);
    }

    @Override
    protected void customizeVendorProperties(Map<String, Object> vendorProperties) {
        super.customizeVendorProperties(vendorProperties);
        vendorProperties.put("javax.persistence.validation.factory", validator);
    }
}

yuvalbo avatar Jan 01 '17 10:01 yuvalbo

@philwebb Please note that HibernateJpaAutoConfiguration constructor has recently changed so for the most current code it should look as following:

@Configuration
public class HibernateValidationConfiguration extends HibernateJpaAutoConfiguration {

  @Autowired
  //Should be avoided but we are overriding an internal spring configuration and cannot change the constructor's signature.
  private Validator validator;

  public HibernateValidationConfiguration(DataSource dataSource,
      JpaProperties jpaProperties,
      ObjectProvider<JtaTransactionManager> jtaTransactionManager,
      ObjectProvider<TransactionManagerCustomizers> transactionManagerCustomizers) {
    super(dataSource, jpaProperties, jtaTransactionManager, transactionManagerCustomizers);
  }

  @Override
  protected void customizeVendorProperties(Map<String, Object> vendorProperties) {
    super.customizeVendorProperties(vendorProperties);
    vendorProperties.put("javax.persistence.validation.factory", validator);
  }
}

yuvalbo avatar Jan 04 '17 11:01 yuvalbo

There's really two parts to this issue, setting the messages and passing the validator to hibernate. Those are now being tracked in #3071 and #15829 respectively.

philwebb avatar Feb 01 '19 16:02 philwebb