Search code examples
spring-3hibernate-validatorspring-4spring-validator

configuration to encode Hibernate Validator customized error messages through Spring Framework


I am working with Spring (4.0.5) and Hibernate Validator (5.1.2.Final)

I already have working the error messages through a .properties file, it works too about i18n.

  • It in a standalone and testing (JUnit) environment, by the moment. I don't want include the Web environment yet to keep the things simple.

I have the following:

For the ValidationMessages_es_PE.properties file (in spanish)

person.salary.digits=Dato invalido '${validatedValue}', máximo tamaño no decimal permitido es {integer}, máximo tamaño decimal permitido es {fraction}

About the infrastructure

@Configuration
public class ValidatorConfiguration {

    @Bean
    public ResourceBundleMessageSource resourceBundleMessageSource(){
        ResourceBundleMessageSource resourceBundleMessageSource = new ResourceBundleMessageSource();
        resourceBundleMessageSource.setDefaultEncoding("UTF-8");
        resourceBundleMessageSource.setBasenames("com.manuel.jordan.validation.messages.ValidationMessages");
        return resourceBundleMessageSource;
    }

    @Bean
    public LocalValidatorFactoryBean localValidatorFactoryBean(){

LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean();
        localValidatorFactoryBean.setValidationMessageSource(resourceBundleMessageSource());

        MessageSourceResourceBundleLocator messageSourceResourceBundleLocator = new MessageSourceResourceBundleLocator(resourceBundleMessageSource()); 
        ResourceBundleMessageInterpolator resourceBundleMessageInterpolator = new ResourceBundleMessageInterpolator(messageSourceResourceBundleLocator);
        localValidatorFactoryBean.setMessageInterpolator(resourceBundleMessageInterpolator);

        return localValidatorFactoryBean;
    }


}

Observe I have setDefaultEncoding("UTF-8")

The test class

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=ValidatorConfiguration.class)
public class InvalidPersonValidation03Test {

    private static final Logger logger = LoggerFactory.getLogger(InvalidPersonValidation03Test.class);

    @Autowired
    private LocalValidatorFactoryBean validator;

    public InvalidPersonValidation03Test(){
        LocaleContextHolder.setLocale(new Locale("es","PE"));
    }

    @Test
    public void testInvalidNullId(){
    ...

Part of the output is the following

- >>>testInvalidFractionSalary
- ConstraintViolationImpl{interpolatedMessage='Dato invalido '1578.100', m�ximo tama�o no decimal permitido es 4, m�ximo tama�o decimal permitido es 2', propertyPath=salary, rootBeanClass=class com.manuel.jordan.domain.PersonValidation03, messageTemplate='{person.salary.digits}'}
- ConstraintViolationImpl{interpolatedMessage='Dato invalido '1578.100', m�ximo tama�o no decimal permitido es 4, m�ximo tama�o decimal permitido es 2', propertyPath=salary, rootBeanClass=class com.manuel.jordan.domain.PersonValidation03, messageTemplate='{person.salary.digits}'}

The characters ñ and á are not showing how it is expected

My IDE already has

Preferences -> Workspace -> Text file encoding with UTF-8

What extra missing configuration I need?


Solution

  • For the community

    I have read this post Problem with Java properties utf8 encoding in Eclipse

    Therefore consider in use this easy and very useful tool ResourceBundle Editor

    It works for Eclipse and Spring Tool Suite

    Problem solved