Custom Message Key in Hibernate validator not working with message.property

折月煮酒 提交于 2019-12-06 15:28:00

Give a try with this, replacing your MessageSource with this:

@Bean
public MessageSource messageSource() {
    ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
    messageSource.setBasenames("classpath:/messages");
    messageSource.setUseCodeAsDefaultMessage(false);
    messageSource.setCacheSeconds((int) TimeUnit.HOURS.toSeconds(1));
    messageSource.setFallbackToSystemLocale(false);
    return messageSource;
}

You have to put your message category.name.notempty=Sorry! Category can't be empty inside resources/messages.properties file

I can mistake with explonation, but I think that The main problem why you cannot resolve validation message key for NotEmpty with Spring MessageSource is Spring Boot auto configuration ValidationAutoConfiguration.

When you are using Spring Boot, it adds org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration where the defaultValidator component which looks something like this:

@Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@ConditionalOnMissingBean(Validator.class)
public static LocalValidatorFactoryBean defaultValidator() {
    LocalValidatorFactoryBean factoryBean = new LocalValidatorFactoryBean();
    MessageInterpolatorFactory interpolatorFactory = new MessageInterpolatorFactory();
    factoryBean.setMessageInterpolator(interpolatorFactory.getObject());
    return factoryBean;
}

As you can see this factoryBean sets MessageInterpolator from MessageInterpolatorFactory using getObject() method which contains code:

return Validation.byDefaultProvider().configure()
                .getDefaultMessageInterpolator();

that returns default Hibernate org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator

Therefore Validator will use default Hibernate ResourceBundleMessageInterpolator to resolve validation message in ValidationMessages.properties file.

If you want to override default Hibernate ResourceBundleMessageInterpolator with own, you need put @Primary to your LocalValidatorFactoryBean:

@Bean
public MessageSource messageSource() {
    final ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
    messageSource.setBasenames("classpath:messages");
    return messageSource;
}

@Bean
@Primary
public LocalValidatorFactoryBean validatorBean(MessageSource messageSource) {
    final LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean();
    bean.setValidationMessageSource(messageSource);
    return bean;
}

And then Validator will use your LocalValidatorFactoryBean with MessageSourceResourceBundleLocator to resolve validation messages in messages.propeties file.

I've had the same problem. but I've solved now. My methods are simple.

  1. DO NOT USE @EnableWebMvc
  2. DO NOT EXTENDS org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport

Good luck.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!