4

I was writing a Spring Boot Application. I wanted to know does Spring Boot automatically resolve message keys in javax and hibernate validation annotations. For example:

@NotEmpty(message = "${message.key}")
String name;

I have provided @PropertySource in my application with message properties file and file is also in my classpath. The keys are resolving with @Value but they are not being resolved in validation annotations.

What could be the reason for this?

Do I need configure a message source bean? Because I have seen examples working without configuring the message source bean.

Miloš Milivojević
  • 5,219
  • 3
  • 26
  • 39
Kiba
  • 399
  • 1
  • 4
  • 16

1 Answers1

3

Are your messages in the correct place? Spring Boot automatically registers a MessageSource bean for you, so you should put your messages in the src/main/resources/messages.properties file. If you have enabled the auto-configuration and also have hibernate-validator dependency on the classpath, everything should work out of the box.

Also @PropertySource is related to application's configuration properties and not messages so the fact that it's not resolving them is to be expected ^^

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
Miloš Milivojević
  • 5,219
  • 3
  • 26
  • 39
  • Thanks for the answer. So the name of the property file name has to be message.properties? What if i want to have a different name for the file or multiple validation messages file? – Kiba Aug 27 '16 at 12:19
  • Should i use spring.messages.basename property if i have a different name file or multiple files. Is there a property file name syntax for it to automatically pickup multiple files? – Kiba Aug 27 '16 at 12:37
  • Yes, that's exactly how you'd override the default file name. You can also name multiple files, the property accepts a comma-separated list – Miloš Milivojević Aug 27 '16 at 12:43
  • Take a look at [this link](http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html) (under internationalization) to see what you configure/override. – Miloš Milivojević Aug 27 '16 at 12:45
  • Thanks for the help. – Kiba Aug 27 '16 at 16:40
  • Note that the messages to resolve must be of the form "{message.key}", not "${message.key}". – thomas.schuerger Jun 25 '21 at 15:18