Spring Boot - custom validation messages

Viewed 669
1 Answers

Firstly, you have to configure messages source. Following the tutorial you have to implement bean like this:

@Bean
   public MessageSource messageSource() {
      ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
      messageSource.setBasename("classpath:messages");
      messageSource.setDefaultEncoding("UTF-8");
      return messageSource;
   }

Where classpath:messages is a base name of messages files placed inside /resource directory. Therefore, you must follow the convention of creating files like messages_{locale}.properties where locale means a specific language tag.

For example, assuming you want to handle english and spanish messages, you must create following files: messages_en.properties and messages_es.properties.

Related