spring-boot: spring boot unable to interpolate message for jsr-303 validation

if I define the following validation messages,

@Size(min = 3, message = "{validation.account.name}")
private String name;

I expect spring boot would read from messages*.properties from classpath and find the validation.account.name key to interpolate the message, but it just shows {validation.account.name} on the page.

This https://github.com/Zane-XY/springboot-i18n-problem demos the problems.

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Reactions: 7
  • Comments: 29 (10 by maintainers)

Commits related to this issue

Most upvoted comments

IMHO it should be easy to implement, like this:

public class MvcConfig extends WebMvcConfigurerAdapter {

    @Inject
    private MessageSource messageSource;

    @Override
    public Validator getValidator() {
        LocalValidatorFactoryBean factory = new LocalValidatorFactoryBean();
        factory.setValidationMessageSource(messageSource);
        return factory;
    }
}

It works.

To get it worked I have to move validation keys into the file ValidationMessages.properties while all oder messages reside in messages.properties.

FYI.

Solved by add ValidationMessages.properties in classpath. this file is used by JSR303 implementation.

Overriding the auto-configured Validator and setting the MessageSource works for me.

ValidationConfig.java

@Configuration
public class ValidationConfig {

    @Bean
    public Validator validator(MessageSource messageSource) {
        LocalValidatorFactoryBean factory = new LocalValidatorFactoryBean();
        factory.setValidationMessageSource(messageSource);
        return factory;
    }
}

Person.java

public class Person {
    @NotNull
    private String name;
}

messages.properties

NotNull.person.name=Name must not be null