I am currently evaluating Spring Data REST.
I started with this simple example: link
It works out of the box. But now, in the next step, I wanted to try some validation to see how the framework reacts. So I simply annotated the Person
class:
@Size(min = 2, message = "{test.error.message}")
private String firstName;
The validation itself is working, I get an error message. The message is resolved if I put a file called ValidationMessages.properties
in the root of the classpath (see here why).
Now, instead of having the files in the root I wanted to place them in a subfolder (e.g. lang/ValidationMessages.properties
) and use Spring MessageSource
instead of the default approach.
After some research I found the following question: MessageInterpolator in Spring
Unfortunately using the following bean definitions does not work:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>lang/ValidationMessages</value>
</list>
</property>
</bean>
<bean id="validator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="validationMessageSource" ref="messageSource"/>
</bean>
</beans>
The corresponding dependencies inside the pom.xml (just in case):
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
Does anyone know what I am missing? Could it be due to the fact that I am not using Spring MVC but Spring Data REST? If yes, is there a way of getting this to work?