Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot - custom validation messages

Can someone please help me? I'm trying to follow this post(https://www.javadevjournal.com/spring-boot/spring-custom-validation-message-source/), to make custom validation messages. I was confused in part 5, which is defining the properties of the file. Do I have to create a file exactly with this nomenclature "messages_ {locale} .properties"? Or do I put the messages inside aplication.properties?

like image 451
GobsRuiz Avatar asked Mar 31 '26 14:03

GobsRuiz


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.

like image 139
Marcin Rzepecki Avatar answered Apr 02 '26 13:04

Marcin Rzepecki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!