1

I have trouble getting custom error messages to work with Spring 3.2. Here is my configs:

Config:

@Configuration
@EnableWebMvc
@EnableJpaRepositories(basePackages = {"com.."})
@EnableTransactionManagement
@ComponentScan(basePackages = {"com.."})
public class Config extends WebMvcConfigurerAdapter {

    @Bean
    public PersistenceExceptionTranslator jpaExceptionTranslator() {
        return new HibernateExceptionTranslator();
    }

    @Bean
    public FactoryBean<EntityManagerFactory> entityManagerFactory(
            DataSource ds, JpaVendorAdapter jva) {
        LocalContainerEntityManagerFactoryBean factoryBean =
                new LocalContainerEntityManagerFactoryBean();
        factoryBean.setPackagesToScan(new String[] { "com.." });
        factoryBean.setDataSource(ds);
        factoryBean.setJpaVendorAdapter(jva);
        factoryBean.afterPropertiesSet();
        return factoryBean;
    }

    @Bean
    public PlatformTransactionManager transactionManager(EntityManagerFactory  emf) {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(emf);
        return transactionManager;
    }

    @Bean
    public ObjectMapper objectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.registerModule(new JodaModule());
        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        return objectMapper;
    }

    @Bean
    public MessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasename("i18n/messages");
        messageSource.setDefaultEncoding("UTF-8");
        return messageSource;
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter converter =
                new MappingJackson2HttpMessageConverter();
        converter.setObjectMapper(objectMapper());
        converters.add(converter);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }

}

And here's messages_en_US.properties file:

Pattern.userdto.email=Invalid email.
Pattern=Invalid syntax.

UserDTO class:

public final class UserDTO {

    @Size(min = MIN_EMAIL_LENGTH, max = MAX_EMAIL_LENGTH)
    @Pattern(regexp = EMAIL_PATTERN)
    private String email;

    ...

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    ...
}

Validation is triggered using javax.validation.Valid annotation.

I'm expecting that this would print out "Invalid email" when the given email doesn't match the pattern, but I'm getting "Invalid syntax" message.

2
  • How are you triggering validation? And how is your validator linked to the message source you configured? Have you set up a LocalValidatorFactoryBean referring to this message source? Commented Jun 10, 2013 at 21:03
  • Validation is triggered using javax.validation.Valid annotation and I have updated my question to include my whole configuration. Commented Jun 10, 2013 at 21:16

2 Answers 2

1

You need to specify the key of the message to be used within the @Pattern constraint:

...
@Pattern(regexp = EMAIL_PATTERN, message = "Pattern.userdto.email")
private String email;
...

Otherwise the default key is used (which is "{javax.validation.constraints.Pattern.message}").

Sign up to request clarification or add additional context in comments.

3 Comments

I thought static.springsource.org/spring/docs/current/javadoc-api/org/… would have resolved the message automatically without specifying the message Pattern.userdto.email on the pattern annotation
Ah, that's interesting. Can such a resolver really be used together with Spring's Bean Validation support? I didn't find any example for that.
Got it working by changing key in message properties file to Pattern.userDTO.email
1

It started working after changing message key to:

Pattern.userDTO.email

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.