2
votes

First time posting here.

I'm developing a JSON webservice API and I'm tring to use custom validation message with no success.

Here is my setup (focus on component involved in my problem. I also use MyBatis, jUnit...) :

  • Tomcat 8.0.14
  • Spring 4.1.1
  • Jersey 2.13
  • Jersey-spring3 2.13
  • Jersey-bean-validation
  • Hibernate Validator 5.0.0.Final

Bean property validation and class-level validation work fine. Now I'm trying to setup custom error validation messages.

I don't wan't to use the JSR-303 /META-INF/ValidationMessages.properties but I wan't to use a spring managed bundle.

In my applicationContext.xml, I defined :

<bean name="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
    <property name="validationMessageSource">
        <ref bean="messageSource" />
    </property>
</bean>

<bean name="messageSource"
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="configuration/messages/validation" />
</bean>

I also create a /WEB-INF/classes/configuration/messages/validation_fr_FR.properties with some properties :

model.error=Modèle invalide

Now when I try to validate my Bean I get this in my logs :

15:47:14,300 DEBUG http-nio-8080-exec-2 resourceloading.PlatformResourceBundleLocator:72 - ValidationMessages not found.
15:47:45,707 DEBUG http-nio-8080-exec-2 resourceloading.PlatformResourceBundleLocator:69 - org.hibernate.validator.ValidationMessages found.

No mention to my bundle here...

In my JSON response, I get :

{model.error} (path = EventServiceAPIImpl.createOrUpdateEvent.arg0, invalidValue = fr.bz.pdcv.bean.Event@d99af88)

{model.error} is not replaced with my custom message.

I think my ReloadableResourceBundleMessageSource is overridden by Hibernate Validator default configuration. But why ?

I overload google and stackoverflow to find a solution, I didn't find anythings which can help me

Does someone encounter the same issue ?

Thank you !

1
I'm too late to your question. But most likely the issue is you are not able to tell the Hibernate Validator what your Locale is. Also by default en_US locale is used by the hibernate validator. So most likely hibernate validator is not able to find the default locale related validationMessage hence you are seeing the error. - n00bc0der

1 Answers

0
votes

Though I guess i am late but it may help someone else.

Rename your validation_fr_FR.properties with ValidationMessages.properties file and put that inside the folder 'src\main\resources'.

So your final path will be as follows:

src\main\resources\ValidationMessages.properties

Restart the server and try to render the validation.

U no more need the LocalValidatorFactoryBean and ResourceBundleMessageSource

Hibernate basically looks for the same file name in the default directory and overwrites the default ValidationMessages.properties