Mybatis Plus日期条件查询传参

本文主要讲解查询操作传入日期字符串无法正确解析时如何解决。

实体类:

/**
* <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方法当中使用,也可以在字段上使用,效果是一样的!

  • 16
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值