java - Hibernate Validator, custom ResourceBundleLocator and Spring -


i'm trying override default resourcebundlelocator in hibernate validation 4.1. far works perfectly, examples of usage include java code instantiate validationfactory.

when using web application spring hibernate validation automatically configured (only suitable hibernate validation *.jar file should exist , automatically used). how can substitute resourcebundlelocator in scenario? not see way of specyfing custom resourcebundlelocator in properties or applicationcontext.xml file.

the magic method required job localvalidatorfactorybean#setvalidationmessagesource(messagesource messagesource).

first of all, contract of method:-

specify custom spring messagesource resolving validation messages, instead of relying on jsr-303's default "validationmessages.properties" bundle in classpath. may refer spring context's shared "messagesource" bean, or special messagesource setup validation purposes only.

note: feature requires hibernate validator 4.1 or higher on classpath. may nevertheless use different validation provider hibernate validator's resourcebundlemessageinterpolator class must accessible during configuration.

specify either property or "messageinterpolator", not both. if build custom messageinterpolator, consider deriving hibernate validator's resourcebundlemessageinterpolator , passing in spring messagesourceresourcebundlelocator when constructing interpolator.

you can specify custom message.properties(or .xml) invoking method... this...

my-beans.xml

<bean name="validator"     class="org.springframework.validation.beanvalidation.localvalidatorfactorybean">     <property name="validationmessagesource">         <ref bean="resourcebundlelocator"/>     </property> </bean>  <bean name="resourcebundlelocator" class="org.springframework.context.support.reloadableresourcebundlemessagesource">     <property name="basenames">         <list>             <value>meta-inf/validation_errors</value>         </list>     </property> </bean> 

validation_errors.properties

javax.validation.constraints.notnull.message=mynotnullmessage 

person.java

    class person {     private string firstname;     private string lastname;      @notnull     public string getfirstname() {         return firstname;     }     public void setfirstname(string firstname) {         this.firstname = firstname;     }     public string getlastname() {         return lastname;     }     public void setlastname(string lastname) {         this.lastname = lastname;     } } 

beanvalidationtest.java

    public class beanvalidationtest {      private static applicationcontext applicationcontext;       @beforeclass     public static void initialize() {         applicationcontext = new classpathxmlapplicationcontext("classpath:meta-inf/spring/webmvc-beans.xml");         assert.assertnotnull(applicationcontext);     }      @test     public void test() {         localvalidatorfactorybean factory = applicationcontext.getbean("validator", localvalidatorfactorybean.class);         validator validator = factory.getvalidator();         person person = new person();         person.setlastname("dude");         set<constraintviolation<person>> violations = validator.validate(person);         for(constraintviolation<person> violation : violations) {             system.out.println("custom message:- " + violation.getmessage());         }     }  } 

outupt: custom message:- mynotnullmessage


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -