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

空扰寡人 提交于 2019-12-08 05:06:14

问题


I am working on Spring Boot, I have used Hibernate Validator to validate my beans. I have added a custom key with the @NotEmpty annotation, and add thos key/value pair in message.properties, but it's not getting its value from message.property

Here is my bean with validation annotation:

public class CategoryBean {
    @NotEmpty(message = "{category.name.notempty}")
    private String name;
    ----getter setter-----
}

message.properties (path:src/main/resources/message.properties)

category.name.notempty=Sorry! Category can't be empty

I have also configured message resource

@Bean
public MessageSource messageSource() {
    ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
    messageSource.setBasename("message");
    return messageSource;
}

I also configured LocalValidatorFactoryBean

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

Whenever I am validating this bean, it's showing its key instead of its value. Hibernate validator default search message key inside ValidationMessages.properties so whenever I am adding ValidationMessages.properties file add there message this key value then it's working fine for me.

But I don't want to add ValidationMessage.properties file, instead of this I want to handle this message key in my message.properties file.

I know there are already some question related this in stack overflow, but those solutions are not solving my problem. I am unable to find out what I am doing wrong here.


回答1:


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




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/43673930/custom-message-key-in-hibernate-validator-not-working-with-message-property

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