【已解决】javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint
javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint
使用hibernate validator出现上面的错误, 一般两种情况:
(1)注解与需要校验的数据类型不匹配,然后就会找不到。譬如在String 类型上使用@Min
org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree#getConstraintValidatorNoUnwrapping
org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorManager#getInitializedValidator
org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorManager#findMatchingValidatorClass
org.hibernate.validator.internal.util.TypeHelper#extractType
org.hibernate.validator.internal.util.TypeHelper#getValidatorsTypes
(2)validation-api和hibernate-validator的版本不匹配。导致注解没有对应的Validator。譬如:validation-api2.x已经收录了@javax.validation.constraints.NotEmpty,但hibernate-validator 5.x中还没有相应的Validator。
hibernate-validator 5.x中是使用组合注解的方式来处理@org.hibernate.validator.constraints.NotEmpty
如下图所示,@javax.validation.constraints.NotEmpty是没有对应的Validator的,如果使用肯定会报 Novalidator
温馨提示:Spring boot 中spring-boot-starter-web 中已经匹配的版本。如果不在pom.xml新增validation-api和hibernate-validator就不会出现版本不匹配
配套的版本:
(1)validation-api 1.1.0.Final + hibernate-validator 5.3.6.Final
(2)validation-api 2.0.1.Final + hibernate-validator 6.0.16.Final
<dependency> <groupId>org.hibernate.validator</groupId> <artifactId>hibernate-validator</artifactId> <version>6.0.16.Final</version> <scope>compile</scope> </dependency>
<dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> <version>2.0.1.Final</version> </dependency>
如果在基本类型上面用NotEmpty或者NotBlank 会出现上面的错
http://blog.csdn.net/dracotianlong/article/details/23181729
javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 'javax.validation.constraints.Size' validating type 'java.lang.Short'. Check configuration for 'status' at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.throwExceptionForNullValidator(ConstraintTree.java:229) at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getConstraintValidatorNoUnwrapping(ConstraintTree.java:310) at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getConstraintValidatorInstanceForAutomaticUnwrapping(ConstraintTree.java:244) at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getInitializedConstraintValidator(ConstraintTree.java:163) at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints(ConstraintTree.java:116) at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateComposingConstraints(ConstraintTree.java:398) at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints(ConstraintTree.java:98) at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints(ConstraintTree.java:87) 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:580) at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForSingleDefaultGroupElement(ValidatorImpl.java:524) at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForDefaultGroup(ValidatorImpl.java:492) at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForCurrentGroup(ValidatorImpl.java:457) at org.hibernate.validator.internal.engine.ValidatorImpl.validateInContext(ValidatorImpl.java:407) at org.hibernate.validator.internal.engine.ValidatorImpl.validate(ValidatorImpl.java:205) at org.springframework.validation.beanvalidation.SpringValidatorAdapter.validate(SpringValidatorAdapter.java:108) at org.springframework.validation.DataBinder.validate(DataBinder.java:866) at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.validateIfApplicable(ModelAttributeMethodProcessor.java:164) at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:111) at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:99) at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:161) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:128) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:817) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:731) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:968) at org.springframework.web.servlet.FrameworkServlet.doPut(FrameworkServlet.java:881) at javax.servlet.http.HttpServlet.service(HttpServlet.java:651) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:844) at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)