Java 异常 Failed to convert property value of type ‘java.lang.String‘ to required type ‘java.util.Date

记录一下自己遇到问题,这是我参考别的博客的,后面我会给到原文链接。

查询时发送给服务器的日期的字符串格式:yyyy-MM-dd HH:mm:ss

服务器接收到日期的字符串之后,向 MySQL 数据库发起查询时,因为没有指定日期时间格式,导致字符串数据不能正确地转换为日期而产生的错误:

 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
 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]
 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 行:

  import java.util.Date;
  
  import org.springframework.format.annotation.DateTimeFormat;
  
  import lombok.AllArgsConstructor;
  import lombok.Data;
  import lombok.NoArgsConstructor;
 
  @NoArgsConstructor // 无参构造方法
  @AllArgsConstructor // 有参构造方法
  @Data // Getters、Setters、toString() 等方法
  public class BillsVo {
 
     @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
     private Date startTime;
     @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
     private Date endTime;
 }

顺便一说,如果要指定服务器端返回给客户端的日期的 JSON 格式:

可以在相应的类的属性上使用 @JsonFormat 注解:

     @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
     @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
     private Date billtime;

如果是 Spring Boot 项目,也可以在 application.yml 文件中指定:

 spring:
   jackson:
     date-format: yyyy-MM-dd HH:mm:ss
     time-zone: GMT+8

如有侵权,请联系删除,谢谢!

原文链接

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值