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

javax.validation.UnexpectedTypeException: HV000030: No validator could be found for type: java.lang.Integer

我必须从表单中分别将 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 的数据类型分别从字符串更改为整数和日期。

我在 Whosebug 上看到了其他答案,他们也使用了 jstl,但我不知道如何在这里应用它。

控制器代码:

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

下面是实体类

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.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)

好吧,如果您没有使用 spring mvc 或 controller 等任何框架,那么您必须自己动手。请求参数总是作为字符串发送到服务器端。现在,某些框架工作是否可以在请求作为拦截器的一部分到达您的控制器之前完成。否则你必须在获得 request.getParameter("param_name")

后手动完成

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

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

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

对于日期,使用 SimpleDateFormat API。

参考文献:

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

希望您像这样在休眠中使用验证,

@NotBlank
private Date some;

@NotBlank 适用于 String 类型。没有 java.util.Date 为空的概念。它可以为空或不为空。请改用 @NotNull

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

有关详细信息,请参阅 Balus answer and @NotBlank

更新

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

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

我不确定,但我怀疑 private short paperNumber; 是问题所在。请求映射正在将其映射到 Integer,不确定在休眠中如何将 Integer 降级为 small int。