编程语言
首页 > 编程语言> > 春季-javax.validation.UnexpectedTypeException:HV000030:找不到以下类型的验证器:java.lang.Integer

春季-javax.validation.UnexpectedTypeException:HV000030:找不到以下类型的验证器:java.lang.Integer

作者:互联网

我必须分别从表单中获取paper_name和date_of_birth的输入作为int和date数据类型.

下面是代码:

<li> Number:<input type="text" name="paper_number" ></li>
< li>date of birth: <input type="text" name="date_of_birth"></ li>

我在控制器中有paperNumber和dateOfBirth变量分别为int和Date数据类型.

请帮助我将paper_number和date_of_birth的数据类型分别从字符串更改为int和date.

我在stackoverflow上看到了其他答案,他们也使用了jstl,但我无法弄清楚如何在这里应用它.

控制器代码:

@RequestMapping(value = "/student" , params = "add", method = RequestMethod.POST )
       public String postAddStudent(
                   @RequestParam("date_of_birth" ) Date date,
                   @RequestParam("paper_number" ) int paperNumber)

下面是EntityClass

public class EntityClass extends BaseEntity {

@Column(name = "DATE_OF_BIRTH", nullable = false)
    @NotBlank
    @DateTimeFormat(pattern = "dd-mm-yyyy")
    private Date dateOfBirth;//TODO

    @Column(name = "PAPER_NUMBER")
    private short paperNumber;
..

}

下面是日志

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is javax.validation.UnexpectedTypeException: HV000030: No validator could be found for type: java.lang.Integer.
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:113)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
root cause

javax.validation.UnexpectedTypeException: HV000030: No validator could be found for type: java.lang.Integer.
    org.hibernate.validator.internal.engine.ConstraintTree.verifyResolveWasUnique(ConstraintTree.java:394)
    org.hibernate.validator.internal.engine.ConstraintTree.findMatchingValidatorClass(ConstraintTree.java:375)
    org.hibernate.validator.internal.engine.ConstraintTree.getInitializedValidator(ConstraintTree.java:333)
    org.hibernate.validator.internal.engine.ConstraintTree.validateConstraints(ConstraintTree.java:155)
    org.hibernate.validator.internal.engine.ConstraintTree.validateConstraints(ConstraintTree.java:125)
    org.hibernate.validator.internal.metadata.core.MetaConstraint.validateConstraint(MetaConstraint.java:86)
    org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraint(ValidatorImpl.java:442)
    org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForDefaultGroup(ValidatorImpl.java:387)
    org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForCurrentGroup(ValidatorImpl.java:351)
    org.hibernate.validator.internal.engine.ValidatorImpl.validateInContext(ValidatorImpl.java:303)
    org.hibernate.validator.internal.engine.ValidatorImpl.validate(ValidatorImpl.java:133)
    org.hibernate.cfg.beanvalidation.BeanValidationEventListener.validate(BeanValidationEventListener.java:136)
    org.hibernate.cfg.beanvalidation.BeanValidationEventListener.onPreInsert(BeanValidationEventListener.java:94)
    org.hibernate.action.internal.EntityIdentityInsertAction.preInsert(EntityIdentityInsertAction.java:178)
    org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:75)
    org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:362)
    org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:203)
    org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:183)
    org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:167)
    org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:320)
    org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:287)
    org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:193)
    org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:126)
    org.hibernate.ejb.event.EJB3MergeEventListener.saveWithGeneratedId(EJB3MergeEventListener.java:71)
    org.hibernate.event.internal.DefaultMergeEventListener.saveTransientEntity(DefaultMergeEventListener.java:236)
    org.hibernate.event.internal.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:216)
    org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:154)
    org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:910)
    org.hibernate.internal.SessionImpl.merge(SessionImpl.java:892)
    org.hibernate.engine.spi.CascadingAction$6.cascade(CascadingAction.java:288)
    org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:380)
    org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:323)
    org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:208)
    org.hibernate.engine.internal.Cascade.cascade(Cascade.java:165)
    org.hibernate.event.internal.AbstractSaveEventListener.cascadeBeforeSave(AbstractSaveEventListener.java:423)
    org.hibernate.event.internal.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:213)
    org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:154)
    org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:76)
    org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:900)
    org.hibernate.internal.SessionImpl.merge(SessionImpl.java:884)
    org.hibernate.internal.SessionImpl.merge(SessionImpl.java:888)
    org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:879)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:497)
    org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:365)
    com.sun.proxy.$Proxy41.merge(Unknown Source)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:497)
    org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240)
    com.sun.proxy.$Proxy41.merge(Unknown Source)
    org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:329)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:497)
    org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:368)
    org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:349)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:155)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
    com.sun.proxy.$Proxy53.save(Unknown Source)
    in.ac.jmi.controllers.AdminController.postAddStudent(AdminController.java:262)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:497)
    org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:113)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)

解决方法:

希望您在休眠状态下使用验证,

@NotBlank
private Date some;

@NotBlank适用于String类型.没有java.util.Date为空的概念.它可以为null或不为null.使用@NotNull代替,

@NotNull
@DateTimeFormat(pattern = "dd-mm-yyyy")
private Date dateOfBirth;

有关更多详细信息,请参见Balus answer@NotBlank.

更新资料

HTTP Status 400: The request sent by the client was syntactically incorrect.

对于上述Date类型的错误,请尝试添加@DateTimeFormat批注,并提及您输入的日期格式(例如yyyy-mm-dd).像下面一样

@RequestMapping(value="/fetch" , method=RequestMethod.GET)
public @ResponseBody String fetchResult(@RequestParam("from")   @DateTimeFormat(pattern="yyyy-MM-dd") Date fromDate) {
    //Content goes here
}

标签:data-conversion,bean-validation,spring,spring-mvc
来源: https://codeday.me/bug/20191027/1943174.html