jackson日期格式转换LocalDateTime异常 JSON parse error: Cannot deserialize value of type `java.time.LocalDate

异常如下

JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime` from String "2023-10-08": Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '2023-10-08' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 2023-10-08 of type java.time.format.Parsed; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.time.LocalDateTime` from String "2023-10-08": Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '2023-10-08' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 2023-10-08 of type java.time.format.Parsed

首先一点,看对应的字段上是否有

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")

如果有,在 filter 或 aop 层次看请求参数中的日期格式。

不排除是请求参数格式与接收的类型冲突。

例如,传的日期是 2023-10-08 却用 LocalDateTime 解析是不行的,应该使用 LocalDate,确保日期格式、日期类型对应起来。

有一个比较好的办法,就是使用long类型传Unix时间戳日期不会有这些问题,还可以做到跨平台跨语言,如果有时区,另外加上时区即可。

https://www.cnblogs.com/seanstudy/p/15848259.html

https://www.cnblogs.com/liuyiyuan/p/16379868.html

https://blog.csdn.net/m0_61871870/article/details/129923962

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
This error occurs when trying to parse a JSON string that contains a value for a LocalDateTime object, but the parsing library is unable to convert it into a LocalDateTime object. To resolve this error, you can try using a JSON parsing library that supports LocalDateTime objects, such as Jackson or Gson. Alternatively, you can modify the JSON string to use a format that can be easily converted to a LocalDateTime object, such as ISO-8601 format. Here's an example of parsing a JSON string containing a LocalDateTime object using Jackson: ``` ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new JavaTimeModule()); // register module to support LocalDateTime objects String json = "{\"dateTime\":\"2021-10-25T10:15:30\"}"; MyClass myObj = mapper.readValue(json, MyClass.class); ``` In this example, the `JavaTimeModule` is registered to support LocalDateTime objects, and the JSON string is parsed into a `MyClass` object. If the JSON string cannot be modified, you may need to manually parse the string and convert it to a LocalDateTime object using a DateTimeFormatter. Here's an example: ``` String json = "{\"dateTime\":\"2021-10-25T10:15:30\"}"; JsonObject jsonObject = JsonParser.parseString(json).getAsJsonObject(); String dateTimeString = jsonObject.get("dateTime").getAsString(); DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME; LocalDateTime dateTime = LocalDateTime.parse(dateTimeString, formatter); ``` In this example, the JSON string is manually parsed using `JsonParser`, the `dateTime` value is retrieved from the `JsonObject`, and a `DateTimeFormatter` is used to convert the string into a `LocalDateTime` object.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值