记springboot一次日期属性没有标注@DateTimeFormat导致前端GET方式传日期值给后端时报错Validation failed for object=...

前端GET请求url参数,携带了fillInDay这个日期值:

?fillInDay=2024-04-17&current=1&size=20&roleCodes=10000

DTO的属性,只是添加了JsonFormat注解:

    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    private LocalDate fillInDay;

前端看到的报错信息
Validation failed for object=‘equipmentMonitoringFillInDTO’. Error count: 1

后端完整报错提示:

Field error in object ‘equipmentMonitoringFillInDTO’ on field ‘fillInDay’: rejected value [2024-04-17]; codes [typeMismatch.equipmentMonitoringFillInDTO.fillInDay,typeMismatch.fillInDay,typeMismatch.java.time.LocalDate,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [equipmentMonitoringFillInDTO.fillInDay,fillInDay]; arguments []; default message [fillInDay]]; default message [Failed to convert property value of type ‘java.lang.String’ to required type ‘java.time.LocalDate’ for property ‘fillInDay’; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@com.fasterxml.jackson.annotation.JsonFormat java.time.LocalDate] for value ‘2024-04-17’; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2024-04-17]]]

解决方案

看着原因是因为日期参数格式和springboot默认解析格式不对应,自己指定日期格式就可以,加上注解@DateTimeFormat并指定日期格式就可以

@DateTimeFormat注解是来自这个包,不要导错:org.springframework.format.annotation.DateTimeFormat

	@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private LocalDate fillInDay;
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值