本文主要讲解查询操作传入日期字符串无法正确解析时如何解决。
实体类:
/**
* <p>开票申请时间
*/
private Date apply_time;
/**
* <p>开票完成时间
*/
private Date complete_time;
Controller:
@GetMapping("/list")
public ReturnResult<?> list(Tricket tricket, ReqParam reqParam) {
return tricketService.listTricket(tricket, reqParam);
}
此时调用接口(/wams/contract-group/list?contract_start_dt=2024-01-01&contract_end_dt=2024-02-05)查询时报错:
org.springframework.validation.BeanPropertyBindingResult: 2 errors\nField error in object 'tricket' on field 'apply_time': rejected value [2024-01-01]; codes [typeMismatch.tricket.apply_time,typeMismatch.apply_time,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [tricket.apply_time,apply_time]; arguments []; default message [apply_time]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'apply_time'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@com.fasterxml.jackson.annotation.JsonFormat java.util.Date] for value '2024-01-01'; nested exception is java.lang.IllegalArgumentException]\nField error in object 'tricket' on field 'complete_time': rejected value [2024-02-02]; codes [typeMismatch.tricket.complete_time,typeMismatch.complete_time,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [tricket.complete_time,complete_time]; arguments []; default message [complete_time]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'complete_time'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@com.fasterxml.jackson.annotation.JsonFormat java.util.Date] for value '2024-02-02'; nested exception is java.lang.IllegalArgumentException]
此提示表明:
处理表单提交时出现了数据类型转换错误,导致无法将字符串类型的日期值转换为 java.util.Date 类型。具体来说,错误发生在 ‘tricket’ 对象的 ‘apply_time’ 和 ‘complete_time’ 字段上。
对于 ‘apply_time’ 字段,错误提示显示被拒绝的值为 ‘2024-01-01’,而程序期望将其转换为 java.util.Date 类型,但转换失败。类似地,对于 ‘complete_time’ 字段,被拒绝的值为 ‘2024-02-02’,转换同样失败。
解决方法:在接收表单数据并进行数据绑定的地方,添加必要的数据转换逻辑,将字符串类型的日期值转换为 java.util.Date 类型。
刚开始给字段添加@JsonFormat(pattern = "yyyy-MM-dd")注解,重新调用接口仍然报错,于是通过百度了解到@DateTimeFormat注解,发现传入方式不同所需要的注解也不同。
区别 | @DateTimeFormat | @JsonFormat |
---|---|---|
使用场景 | URL传参时,格式化前端传向后端日期类型的时间格式 | JSON传参,格式化前端传参和后端返回给前端的时间格式,传参可能不一定是json,但是一般接口向前端返回数据,基本上都是封装的统一返回格式,然后JSON返回。所以这个注解是一定要加的! |
使用方法 | @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") | @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") |
使用地方 | 实体类日期字段上、或者字段的set方法上、或者方法入参上 | 实体类日期字段上、或者字段的set方法上、、或者方法入参上 |
来源 | org.springframework.format.annotation | com.fasterxml.jackson.annotation |
注意:
一旦使用yyyy-MM-dd 格式,如果传时分秒就会报错,或者是使用 yyyy-MM-dd HH:mm:ss,如果传yyyy-MM-dd 也会报错。
假如是springboot项目的话,使用这两个注解是不用导其他的依赖包的!
框架当中默认他会认为 前端传的是UTC时间,然后SpringMVC在接到参数的时候,会进行转换为本地区时间,向前端返回参数的时候会转换为UTC时间!
这两个注解可以选择在实体类的set方法当中使用,也可以在字段上使用,效果是一样的!