Java 异常 Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date'
查询时发送给服务器的日期的字符串格式:yyyy-MM-dd HH:mm:ss

服务器接收到日期的字符串之后,向 MySQL 数据库发起查询时,因为没有指定日期时间格式,导致字符串数据不能正确地转换为日期而产生的错误:
1 2019-12-05 17:43:55.215 WARN 1460 --- [nio-8080-exec-6] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 2 errors
2 Field error in object 'billsVo' on field 'endTime': rejected value [2019-12-05 00:00:00]; codes [typeMismatch.billsVo.endTime,typeMismatch.endTime,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [billsVo.endTime,endTime]; arguments []; default message [endTime]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'endTime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.util.Date] for value '2019-12-05 00:00:00'; nested exception is java.lang.IllegalArgumentException]
3 Field error in object 'billsVo' on field 'startTime': rejected value []; codes [typeMismatch.billsVo.startTime,typeMismatch.startTime,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [billsVo.startTime,startTime]; arguments []; default message [startTime]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'startTime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.util.Date] for value ''; nested exception is java.lang.IllegalArgumentException]]
解决方法:在相应的属性上使用 @DateTimeFormat 注解,并指定格式,见第 14 或 16 行:
1 import java.util.Date;
2
3 import org.springframework.format.annotation.DateTimeFormat;
4
5 import lombok.AllArgsConstructor;
6 import lombok.Data;
7 import lombok.NoArgsConstructor;
8
9 @NoArgsConstructor // 无参构造方法
10 @AllArgsConstructor // 有参构造方法
11 @Data // Getters、Setters、toString() 等方法
12 public class BillsVo {
13
14 @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
15 private Date startTime;
16 @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
17 private Date endTime;
18 }
顺便一说,如果要指定服务器端返回给客户端的日期的 JSON 格式:

可以在相应的类的属性上使用 @JsonFormat 注解:
1 @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
2 @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
3 private Date billtime;
如果是 Spring Boot 项目,也可以在 application.yml 文件中指定:
1 spring:
2 jackson:
3 date-format: yyyy-MM-dd HH:mm:ss
4 time-zone: GMT+8

Java 异常 Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date'的更多相关文章
- spring mvc出现 Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'endtime'
在使用spring mvc中,绑定页面传递时间字符串数据给Date类型是出错: Failed to convert property value of type [java.lang.String] ...
- 【spring mvc】后台API查询接口,get请求,后台Date字段接收前台String类型的时间,报错default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'createDate';
后台API查询接口,get请求,后台Date字段接收前台String类型的时间筛选条件 后台接口接收 使用的实体 而createDate字段在后台实体中是Date类型 报错信息: org.spring ...
- 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.cor ...
- Cannot convert value of type [java.lang.String] to required type [javax.sql.DataSource] for property 'dataSource': no matching editors or conversion strategy found
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFa ...
- 解决spring mvc 上传报错,Field [] isn't an enum value,Failed to convert value of type 'java.lang.String[]' to required type '
没有选择附件,但是点击上传按钮的时候会报错. 之前不选择文件,直接上传空文件是可以的,后来不知道改了什么就不行了. 错误信息: -- :: [http--] TRACE org.springframe ...
- Failed to convert value of type 'java.lang.String' to required type 'java.time.LocalDate';
springboot jdbc查询使用LocalDate报:Failed to convert value of type 'java.lang.String' to required type 'j ...
- SpringBoot 项目启动 Failed to convert value of type 'java.lang.String' to required type 'cn.com.goldenwater.dcproj.dao.TacPageOfficePblmListDao';
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'tac ...
- 完美解决报错Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'
Failed to convert value of type 'java.lang.String' to required type 'java.util.Date' 首先这个错误的意思是 前台页面 ...
- 前台传参数时间类型不匹配:type 'java.lang.String' to required type 'java.util.Date' for property 'createDate'
springMVC action接收参数: org.springframework.validation.BindException: org.springframework.validation.B ...
随机推荐
- 【Android】java生成炫酷验证码,不区分大小写。登陆,发送手机验证码,防止注册机,android开发
作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985 QQ986945193 微博:http://weibo.com/mcxiaobing 首先给大家看一下 ...
- 关于List的remove()方法
最近遇到一个小问题,我将其简化为下列代码,List的remove()方法在下列颜色注重的代码执行的源码也是不同的~ List<Integer> list=new ArrayList< ...
- MySQL 外部联结 内连接、左右外连接辨析
内连接 在进行跨表内连接查询数据时,查询结果只返回符合查询条件的数据:跨表内连接查询的结果和使用where的多表查询结果相同,其实就是普通的查询,没啥好说的 -- 语法: SELECT 别名1.字段名 ...
- js map数据结构
Map 对象保存键值对,并且能够记住键的原始插入顺序.任何值(对象或者原始值) 都可以作为一个键或一个值. map对象常用于保存键值对,它的键是任意数据类型,常用于建立数据的映射关系 和对象的区别: ...
- 转贴:110个Oracle 函数
转载地址:https://bbs.csdn.net/topics/310021870 1. ASCII返回与指定的字符对应的十进制数;SQL> select ascii(A) A,ascii(a ...
- 高德地图POI爬取_Python
高德地图POI 官方文档:https://lbs.amap.com/api/webservice/guide/api/search#introduce 官网控制台:https://lbs.amap.c ...
- 没有修改getModel()方法的返回值导致的Hibernate接收不到页面数据
异常1.通过id进行查询,但id为null,就出现这个异常!java.lang.IllegalArgumentException: id to load is required for loading ...
- 蓝奏网盘API
蓝奏云网盘API 2.0 基于Python3实现,最强的蓝奏云API~ 蓝奏云注册 更新说明 修复了登录时 formhash 错误的问题 解决了多次上传大文件被限制的问题 #3 细化 API 接口的功 ...
- JZOJ1495 宝石
Description 见上帝动了恻隐之心,天后也想显示一下慈悲之怀,随即从口袋中取出一块魔术方巾,让身边的美神维纳斯拿到后堂的屏风上去试试,屏风是正方形的,高和宽方向上各划有m条鱼屏风的边平行的直线 ...
- IDEA中Javaweb项目图片加载不出来解决方案
针对IDEA中Javaweb项目中无法加载图片 一.项目的结构及问题介绍 项目结构如下: 在500.jsp页面中访问了img目录下的相关图片,比如背景图片,我在500.jsp中是这样写的路径 bac ...