Javax.validation.UnexpectedTypeException:HV000030:No Validator could is found for constraint workaround

Source: Internet
Author: User

When using hibernate validator for parameter validation, we sometimes encounter javax.validation.UnexpectedTypeException:HV000030:No validator could be Found for constraint, such as in our application, uses custom annotations Dict to validate the enumeration, because the defined dict is applied to the string type, for example:

 Packagecom.yidoo.base.metadata.validate;Importjava.lang.annotation.Documented;ImportJava.lang.annotation.ElementType;Importjava.lang.annotation.Retention;ImportJava.lang.annotation.RetentionPolicy;ImportJava.lang.annotation.Target;ImportJavax.validation.Constraint;Importjavax.validation.Payload; @Target ({Elementtype.field}) @Retention (value=retentionpolicy.runtime) @Constraint (Validatedby= {Dictvalidator.class}) @Documented Public@InterfaceDict {/*** The standard name of the data dictionary, camel or underline can be separated *@return     */String dictname (); /*** indicates that the validation data dictionary validity range is explicitly specified for the field name.     <br/> * For some special scenarios, such as invalid order status, but these effectiveness we do not want the business order to explicitly pass this value, <br/> * So to filter out, for reusing the data dictionary, but the value range is a subset of the case. * default is NULL, indicating that all child items are valid.     The equivalent is valid unless the declaration is valid. * @return     */String fieldName ()default""; String message ()default"{Data dictionary value is not valid, refer to standard data dictionary management}"; Class<?>[] Groups ()default {}; Class<?extendsPayload>[] Payload ()default {};}
 Packagecom.yidoo.base.metadata.validate;ImportJavax.validation.ConstraintValidator;ImportJavax.validation.ConstraintValidatorContext;Importcom.yidoo.base.metadata.cons.DictUtils; Public classDictvalidatorImplementsConstraintvalidator<dict, string> {        PrivateString Dictname; @Override Public voidInitialize (Dict dictanno) { This. Dictname =Dictanno.dictname (); } @Override Public BooleanisValid (String value, Constraintvalidatorcontext context) {if(Dictutils.isvalid (dictname, value)) {return true;        } context.disabledefaultconstraintviolation (); Context.buildconstraintviolationwithtemplate (The current value of the field + value + is not in the valid range of data dictionary + dictname +, valid values are: ["+ Dictutils.getdictkeys (dictname) +"] "). Addconstraintviolation (); return false; }}

It will fail in the following check:

    @NotNull    = ioperatorservice.save)    @Dict (dictname= "Bool_int")      Private Integer Loginflag;

As follows:

Javax.validation.UnexpectedTypeException:HV000030:No Validator could be found forConstraint ' com.yidoo.base.metadata.validate.Dict ' validating type ' Java.lang.Integer '. Check Configuration for' Firsttimeflag 'At  Org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.throwExceptionForNullValidator ( Constrainttree.java:228) at Org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getConstraintValidatorNoUnwrapping ( Constrainttree.java:309) at Org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getConstraintValidatorInstanceForAutomaticUnw Rapping (Constrainttree.java:243) at Org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getInitializedConstraintValidator ( Constrainttree.java:164) at Org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints ( Constrainttree.java:109) at Org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints ( Constrainttree.java:88) at Org.hibernate.validator.internal.metadata.core.MetaConstraint.validateConstraint (Metaconstraint.java: 73) at Org.hibernate.validator.internal.engine.ValidatorImpl.validateMetaConstraint (Validatorimpl.java:617) at Org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraint (Validatorimpl.java:582) at Org.hibernate.validator.internal.engine.ValidatorImpl.validatePropertyForSingleDefaultGroupElement ( Validatorimpl.java:1051) at Org.hibernate.validator.internal.engine.ValidatorImpl.validatePropertyForDefaultGroup ( Validatorimpl.java:1019) at Org.hibernate.validator.internal.engine.ValidatorImpl.validatePropertyForCurrentGroup ( Validatorimpl.java:936) at Org.hibernate.validator.internal.engine.ValidatorImpl.validatePropertyInContext (Validatorimpl.java: 824) at Org.hibernate.validator.internal.engine.ValidatorImpl.validateProperty (Validatorimpl.java:223) at Com.yidoo.utils.ValidationUtils.validatePropertyInternal (Validationutils.java:56) at Com.yidoo.utils.ValidationUtils.validateEntityByServiceId (Validationutils.java:99) at Com.yidoo.utils.LfValidUtil.newValid (Lfvalidutil.java:37) at Com.yidoo.k3c.base.service.ClientService.insert (Clientservice.java:129)        

When encountering some design, the value is a Boolean type, 0 means that the false,1 represents true when the integer type is used, the dict annotation will occur when the above error, for the exception, because the Dict validator parameter string does not match the integer, To resolve this exception, we can then define a intdict for the integer validation, as follows:

 Packagecom.yidoo.base.metadata.validate;Importjava.lang.annotation.Documented;ImportJava.lang.annotation.ElementType;Importjava.lang.annotation.Retention;ImportJava.lang.annotation.RetentionPolicy;ImportJava.lang.annotation.Target;ImportJavax.validation.Constraint;Importjavax.validation.Payload; @Target ({Elementtype.field}) @Retention (value=retentionpolicy.runtime) @Constraint (Validatedby= {Intdictvalidator.class}) @Documented Public@Interfaceintdict {/*** The standard name of the data dictionary, camel or underline can be separated *@return     */String dictname (); /*** indicates that the validation data dictionary validity range is explicitly specified for the field name.     <br/> * For some special scenarios, such as invalid order status, but these effectiveness we do not want the business order to explicitly pass this value, <br/> * So to filter out, for reusing the data dictionary, but the value range is a subset of the case. * default is NULL, indicating that all child items are valid.     The equivalent is valid unless the declaration is valid. * @return     */String fieldName ()default""; String message ()default"{Data dictionary value is not valid, refer to standard data dictionary management}"; Class<?>[] Groups ()default {}; Class<?extendsPayload>[] Payload ()default {};}
 Packagecom.yidoo.base.metadata.validate;ImportJavax.validation.ConstraintValidator;ImportJavax.validation.ConstraintValidatorContext;Importcom.yidoo.base.metadata.cons.DictUtils; Public classIntdictvalidatorImplementsConstraintvalidator<intdict, integer> {        PrivateString Dictname; @Override Public voidInitialize (intdict dictanno) { This. Dictname =Dictanno.dictname (); } @Override Public BooleanisValid (Integer value, Constraintvalidatorcontext context) {if(Dictutils.isvalid (Dictname, string.valueof (value))) {return true;        } context.disabledefaultconstraintviolation (); Context.buildconstraintviolationwithtemplate (The current value of the field + value + is not in the valid range of data dictionary + dictname +, valid values are: ["+ Dictutils.getdictkeys (dictname) +"] "). Addconstraintviolation (); return false; }}

Note: Why not use regular calibration directly? Because for many enumeration types, the range of values is defined by the business randomization, which may depend on constants, enumeration classes, or even dynamically loaded into the cached definition for validation (see design conventions and specifications for different systems).

Javax.validation.UnexpectedTypeException:HV000030:No Validator could is found for constraint workaround

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.