Friday 15 July 2011

java - Hibernate validator to validate constraints if @Constraint(validated by = {}) -



java - Hibernate validator to validate constraints if @Constraint(validated by = {}) -

i have spring webapplication uses hibernate validator validation. have constraint annotations located in different project. need have validators these constraints in spring project because need services perform validation. situation is: cannot place constraint validators in @constraint(validatedby = myconstraintvalidator.class) because validator in different project, cannot add together dependency because create cyclic dependency , not way want it. noticed javax.constraints don't have validators specified, it's @constraint(validatedby= {}). hibernate validator still knows how validate them. there way tell hibernate validator how validate constraints without specifying constaint-validators in constraints?

i hope able validate constraint:

@retention(retentionpolicy.runtime) @target(elementtype.field) @constraint(validatedby = {}) public @interface oneof { string message() default "{oneof}"; class<?>[] groups() default {}; class<? extends payload>[] payload() default {}; }

thanks!

the javax.validation.constraints leaves validators implementation. hibernate provides validators these constraints, , registers them when bootstrapping.

you can set validators via xml without using @constraint(validatedby = { }). however, used add together to/replace provided validators own. i'm not sure how help since still need reference constraint validator.

example of using xml constraint definition:

<constraint-definition annotation="org.mycompany.checkcase"> <validated-by include-existing-validators="false"> <value>org.mycompany.checkcasevalidator</value> </validated-by> </constraint-definition>

see: configuring via xml

the upcoming 5.2 release provides more ways such using service loader or implementing constraintdefinitioncontributor. see: providing constraint definitions

java spring-mvc constraints hibernate-validator

No comments:

Post a Comment