【错误】Cannot construct instance of `java.time.Instant`或 illeal localdatetime string : 2024-03-29 17:01


前言

项目中使用的日期格式是:private Instant time;
当进行反序列化时,报错:Cannot construct instance of ‘java.time.Instant’ 或 illeal localdatetime string : 2024-03-29 17:01:42


提示:以下是本篇文章正文内容,下面案例可供参考

一、错误详情

  • 使用 objectMapper 报错:Cannot construct instance of java.time.Instant 错误

InfluxdbRespDto vo = new ObjectMapper().convertValue(rCommonPort.getData(), InfluxdbRespDto.class);

java.lang.IllegalArgumentException: Cannot construct instance of `java.time.Instant` (no Creators, like default construct, exist): no String-argument constructor/factory method to deserialize from String value ('2024-03-29 16:52:22')
 at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: com.sty.common.dto.influxdb.InfluxdbRespDto["commonPortResList"]->java.util.ArrayList[0]->com.sty.common.model.influxdb.response.CommonPortRes["time"])
	at com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:3938) ~[jackson-databind-2.10.3.jar:2.10.3]
	at com.fasterxml.jackson.databind.ObjectMapper.convertValue(ObjectMapper.java:3869) ~[jackson-databind-2.10.3.jar:2.10.3]
  • 使用 fastjson 报错:illeal localdatetime string : 2024-03-29 17:01:42

InfluxdbRespDto vo = JSON.parseObject(JSON.toJSONString(rCommonPort.getData()), InfluxdbRespDto.class);

com.alibaba.fastjson.JSONException: illeal localdatetime string : 2024-03-29 17:01:42
	at com.alibaba.fastjson.JSON.parseObject(JSON.java:108) ~[fastjson-2.0.0.jar:na]
	at com.alibaba.fastjson.JSON.parseObject(JSON.java:87) ~[fastjson-2.0.0.jar:na]

二、解决方案

1.使用 objectMapper

ObjectMapper objectMapper = new ObjectMapper()
                        .findAndRegisterModules()
                        .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
InfluxdbRespDto vo = objectMapper.convertValue(rCommonPort.getData(), InfluxdbRespDto.class);

总结

拥有时,不管再欢喜,都好似一边欢喜,一边有另一个自己在空中俯瞰着自己,提醒着自己失去。因为这份清醒理智,纵使欢喜也带着隐隐的伤感,而真正失去时,因为早有准备,纵使难过也会平静的接受。——长相思

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值