Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'xxx': no matching editors or conversion strategy found

简介:

今天在完成项目的时候遇到了下面的异常信息:

04-Aug-2014 15:49:27.894 SEVERE [http-apr-8080-exec-5] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [cms] in context with path [/cms] threw exception [Request processing failed; nested exception is org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 6 errors
Field error in object 'carContractSearchBox' on field 'createEndDate': rejected value []; codes [typeMismatch.carContractSearchBox.createEndDate,typeMismatch.createEndDate,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [carContractSearchBox.createEndDate,createEndDate]; arguments []; default message [createEndDate]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'createEndDate'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'createEndDate': no matching editors or conversion strategy found]

先说明一下,我们的项目使用的是Spring MVC。相应的功能是一个简单的form表单查询功能,里面有一些日期字段的查询。

相应的解决办法为:

在对应的controller中增加属性编辑器:

@InitBinder
protected void initBinder(WebDataBinder binder) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}

注意这块的new CustomDateEditor(dateFormat, true)中的true,查看CustomDateEditor源码可以看到:

/**
 * Create a new CustomDateEditor instance, using the given DateFormat
 * for parsing and rendering.
 * <p>The "allowEmpty" parameter states if an empty String should
 * be allowed for parsing, i.e. get interpreted as null value.
 * Otherwise, an IllegalArgumentException gets thrown in that case.
 * @param dateFormat DateFormat to use for parsing and rendering
 * @param allowEmpty if empty strings should be allowed
 */
public CustomDateEditor(DateFormat dateFormat, boolean allowEmpty) {
    this.dateFormat = dateFormat;
    this.allowEmpty = allowEmpty;
    this.exactDateLength = -1;
}

allowEmpty字段为true的时候form表单传递的值可以为空。否则会出现""字符串解析为date报错。

目录
相关文章
|
数据采集 Cloud Native Java
在 GraalVM 静态编译下无侵入实现可观测探索
在 GraalVM 静态编译下无侵入实现可观测探索
112780 120
|
Kubernetes Dubbo 应用服务中间件
Dubbo3实践:基于 API-SERVER 的原生 K8S Service
> 该示例演示了直接以 API-SERVER 为注册中心,将 Dubbo 应用部署到 Kubernetes 并复用 Kubernetes Native Service 的使用示例。 > > 此示例的局限在于需要授予每个 Dubbo 应用访问 API-SERVER 特定资源的权限,同时直接访问和监听 API-SERVER 对中小集群来说并没有什么问题,但对于较大规模集群而言可能给 API-SERV
753 0
|
缓存 算法 Java
【开发利器Hutool】推荐一个超好用的本地缓存
【开发利器Hutool】推荐一个超好用的本地缓存
2379 0
【开发利器Hutool】推荐一个超好用的本地缓存
|
存储 安全 Java
Spring Security 6.x OAuth2登录认证源码分析
上一篇介绍了Spring Security框架中身份认证的架构设计,本篇就OAuth2客户端登录认证的实现源码做一些分析。
932 2
Spring Security 6.x OAuth2登录认证源码分析
|
XML JSON Java
SpringMVC获取请求中的参数值不同方式总结
SpringMVC获取请求中的参数值不同方式总结
288 0
|
移动开发 前端开发 Java
第一次用java17记录运行ruoyi-vue-plus5.X版本
第一次用java17记录运行ruoyi-vue-plus5.X版本
342 0
|
JSON 前端开发 Java
【SpringBoot实战专题】「开发实战系列」全方位攻克你的技术盲区之Spring定义Jackson转换Null的方法和实现案例
【SpringBoot实战专题】「开发实战系列」全方位攻克你的技术盲区之Spring定义Jackson转换Null的方法和实现案例
305 0
|
SQL 数据库
详解BaseMapper
详解BaseMapper
683 0
|
Java 大数据 Spring
MyBatis-Plus - 批量插入、更新、删除、查询
MyBatis-Plus - 批量插入、更新、删除、查询
2612 0
|
人工智能 Kubernetes 大数据
探索云原生容器编排技术:如Kubernetes如何为大数据处理和AI模型的自动化部署带来便利
Kubernetes以容器为基础,将应用程序和其依赖项封装在容器中。这使得大数据处理和AI模型的部署更加一致和可移植,可以在不同的环境中轻松部署,包括开发、测试和生产环境。
520 0