java - Custom Bean Validation UnexpectedType Exception Spring 3.2 -
i'm new application uses spring 3.2. project upgrade , upgraded java 1.6 1.7. after upgrading i'm facing issue custom bean validation on group of field.
i had google , understand validation , looks good. please note works java 1.6 , made code comparison too.
controller.java
@requestmapping(value = constants.url, method = requestmethod.post) public modelandview process( @modelattribute("bean") bean bean, bindingresult result, sessionstatus status, @requestparam(constants.wizard_page) int currentpage, modelmap modelmap, model model, locale locale, redirectattributes redirectattributes, httpservletrequest request, httpservletresponse response) { isnotvalid(bean, result, interface.class); } public boolean isnotvalid(object target, errors errors, class<?>... groups) { set<constraintviolation<object>> result = validator.validate(target, groups); bean class @notempty @phonecomposite(phone1 = "phone1", phone2 = "phone2", phone3 = "phone3", groups = { interface.class, interface2.class }) private phone phone = new phone(); phone bean class public phone() { super(); } public phone(string phone1, string phone2, string phone3) { this(); this.phone1 = phone1; this.phone2 = phone2; this.phone3 = phone3; } //having getter , setter
constraint class
@constraint(validatedby = { phonecompositevalidator.class }) @documented @target({ elementtype.field, elementtype.annotation_type, elementtype.method }) @retention(retentionpolicy.runtime) public @interface phonecomposite { /** * message. * * @return string */ string message() default "{invalid.phone}"; /** * groups. * * @return class[] */ class<?>[] groups() default {}; /** * payload. * * @return class<? extends payload>[] */ class<? extends payload>[] payload() default {}; /** * phone1. * * @return string */ string phone1(); /** * phone2. * * @return string */ string phone2(); /** * phone3. * * @return string */ string phone3(); } validator class public class phonecompositevalidator implements constraintvalidator<phonecomposite, object>{ /** phone1 name. */ private string phone1name; /** phone2 name. */ private string phone2name; /** phone3 name. */ private string phone3name; /** * initialize. * * @param arg0 arg0 */ @override public void initialize(phonecomposite arg0) { phone1name = arg0.phone1() ; phone2name = arg0.phone2() ; phone3name = arg0.phone3() ; } /* (non-javadoc) * @see javax.validation.constraintvalidator#isvalid(java.lang.object, javax.validation.constraintvalidatorcontext) */ /** * checks if valid. * * @param arg0 arg0 * @param arg1 arg1 * @return true, if valid */ @override public boolean isvalid(object arg0, constraintvalidatorcontext arg1) { string phone1 = null ; string phone2 = null ; string phone3 = null ; try { phone1 = beanutils.getproperty(arg0, phone1name) ; if (stringutils.isempty(phone1) || (!phone1.matches("\\d{3}"))) { loggerservice.error("phone1 not valid"); return false ; } phone2 = beanutils.getproperty(arg0, phone2name) ; if (stringutils.isempty(phone2) || (!phone2.matches("\\d{3}"))) { loggerservice.error("phone2 not valid"); return false ; } phone3 = beanutils.getproperty(arg0, phone3name) ; if (stringutils.isempty(phone3) || (!phone3.matches("\\d{4}"))) { loggerservice.error("phone3 not valid"); return false ; } return true ; } catch (exception e) { e.printstacktrace() ; return false; } } }
usage of libraries
<dependency org="javax.validation" name="validation-api" rev="1.0.0.ga" transitive="false"> <artifact name="validation-api" ext="jar"/> </dependency> <dependency org="org.slf4j" name="slf4j-api" rev="1.7.5" transitive="false"> <artifact name="slf4j-api" ext="jar"/> </dependency> <dependency org="org.slf4j" name="slf4j-log4j12" rev="1.7.5" transitive="false"> <artifact name="slf4j-log4j12" ext="jar"/> </dependency>
note: working in java 1.6 , no code change made upgrade.
exception:
caused by: javax.validation.unexpectedtypeexception: no validator found type bean.phone. see: @size @ private bean.phone bean.bean.phone @ org.apache.bval.jsr303.annotationprocessor.checkonetype(annotationprocessor.java:328) @ org.apache.bval.jsr303.annotationprocessor.getconstraintvalidator(annotationprocessor.java:303) @ org.apache.bval.jsr303.annotationprocessor.applyconstraint(annotationprocessor.java:243) @ org.apache.bval.jsr303.annotationprocessor.processannotation(annotationprocessor.java:151) @ org.apache.bval.jsr303.annotationprocessor.processannotations(annotationprocessor.java:92) @ org.apache.bval.jsr303.annotationprocessor.applyconstraint(annotationprocessor.java:279) @ org.apache.bval.jsr303.annotationprocessor.processannotation(annotationprocessor.java:151) @ org.apache.bval.jsr303.annotationprocessor.processannotations(annotationprocessor.java:92) @ org.apache.bval.jsr303.jsr303metabeanfactory.processclass(jsr303metabeanfactory.java:134) @ org.apache.bval.jsr303.jsr303metabeanfactory.buildmetabean(jsr303metabeanfactory.java:95) @ org.apache.bval.metabeanbuilder.buildforclass(metabeanbuilder.java:129) @ org.apache.bval.metabeanmanager.findforclass(metabeanmanager.java:102) @ org.apache.bval.jsr303.classvalidator.validate(classvalidator.java:140) @ org.springframework.validation.beanvalidation.springvalidatoradapter.validate(springvalidatoradapter.java:199)
please me find out jar might referring on dependency i'm suspecting java version.
Comments
Post a Comment