javax.validation.UnexpectedTypeException:HV000030:找不到类型的验证程序:java.lang.Integer

时间:2016-03-04 09:28:09

标签: spring spring-mvc bean-validation data-conversion

我必须分别从表单中将paper_namedate_of_birth作为intdate数据类型输入。

以下是代码:

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

我在控制器中有paperNumberdateOfBirth变量,分别为int和Date数据类型。

请帮我将paper_number和date_of_birth的数据类型从string更改为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)

4 个答案:

答案 0 :(得分:4)

希望你在这样的hibernate中使用验证,

@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状态400:客户端发送的请求在语法上不正确。

对于日期类型中的上述错误,请尝试添加@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
}

答案 1 :(得分:0)

如果你没有使用像spring mvc或controller这样的任何框架,那么你必须自己动手。请求参数始终作为字符串发送到服务器端。现在,在请求作为拦截器的一部分进入控制器之前,某些框架是否可以执行此操作。否则,您必须在获得request.getParameter("param_name")

后手动执行此操作

答案 2 :(得分:0)

如果要限制用户输入的输入,则必须使用Javascript来限制输入类型(您可以使用jQuery验证器插件)。

否则,如果要将用户输入的输入转换为整数和日期,在表单操作(可能是servlet)中,则需要使用

解析整数
Integer.parseInt(request.getParameter("integer_field_name"));

对于Date,请使用SimpleDateFormat API。

参考文献:

  1. How to convert String to Date
  2. jQuery validator plugin

答案 3 :(得分:0)

我不确定,但我怀疑是private short paperNumber;是问题..请求映射是将它映射到Integer,不确定如何在hibernate中将Integer降级为小int。