当前位置:网站首页>com. fasterxml. jackson. databind. Exc.invalidformatexception problem
com. fasterxml. jackson. databind. Exc.invalidformatexception problem
2022-07-03 03:12:00 【Friendship years】
Client side usage fastjson serialize , Server use jackson Deserialization failure . Report errors .
Caused by: com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.util.Date` from String "2022-04-04 04:04:04": not a valid representation (error: Failed to parse Date value '2022-04-04 04:04:04': Cannot parse date "2022-04-04 04:04:04": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSZ', parsing fails (leniency? null))
reason :
fastjson The default serialization format :
public static String DEFFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
jackson The default supported date inverse sequence format :
("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd"))
jackson The default date inverse sequence does not support yyyy-MM-dd HH:mm:ss This format , So deserialization failed
Solution one : rewrite jackson Deserialization
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @version 1.0.0
* @description Override deserialization
* @date 2018/12/25 9:51
**/
public class CustomJsonDateDeserializer extends JsonDeserializer<Date> {
@Override
public Date deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = jp.getText();
try {
return format.parse(date);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
}
In need of date Add comments to attributes :
@JsonDeserialize(using = CustomJsonDateDeserializer.class)
private Date createTime;
In this way , You need to override the above deserialization abstract class
Solution two : Use it directly jackson annotation , No need to override deserialization
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date createTime;
GMT+8 Time zone , East eight
Solution three : Use it directly fastjson annotation , Do not override deserialization
@JSONField(format = “yyyy-MM-dd HH:mm:ss”)
private Date createTime;
Add dependency :
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.32</version>
</dependency>
<!-- jackson-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.4</version>
</dependency>
边栏推荐
- I2C subsystem (III): I2C driver
- MySQL practice 45 [SQL query and update execution process]
- I2C 子系統(四):I2C debug
- MySQL Real combat 45 [SQL query and Update Execution Process]
- Spark on yarn resource optimization ideas notes
- 基于QT的tensorRT加速的yolov5
- Destroy the session and empty the specified attributes
- Thunderbolt Chrome extension caused the data returned by the server JS parsing page data exception
- QT based tensorrt accelerated yolov5
- MySql实战45讲【全局锁和表锁】
猜你喜欢
![45 lectures on MySQL [index]](/img/f6/70be00028908cbd9ed7f2c77687cee.png)
45 lectures on MySQL [index]

Idea set method call ignore case

VS 2019配置tensorRT

Installation and use of memory leak tool VLD

On the adjacency matrix and adjacency table of graph storage

PAT乙级“1104 天长地久”DFS优化思路

Hi3536C V100R001C02SPC040 交叉编译器安装

C language beginner level - pointer explanation - paoding jieniu chapter

900W+ 数据,从 17s 到 300ms,如何操作

I2C subsystem (II): I3C spec
随机推荐
Do you really understand relays?
Yiwen takes you to know ZigBee
Practice of traffic recording and playback in vivo
Last update time of all sqlserver tables
Destroy the session and empty the specified attributes
Privatization lightweight continuous integration deployment scheme -- 01 environment configuration (Part 2)
解决高并发下System.currentTimeMillis卡顿
将时间戳转为指定格式的时间
How to make backgroundworker return an object
open file in 'w' mode: IOError: [Errno 2] No such file or directory
About HTTP cache control
Docker install redis
js根据树结构查找某个节点的下面的所有父节点或者子节点
I2C 子系統(四):I2C debug
Idea set method call ignore case
[combinatorics] number of solutions of indefinite equations (number of combinations of multiple sets R | number of non negative integer solutions of indefinite equations | number of integer solutions
Use optimization | points that can be optimized in recyclerview
labelimg生成的xml文件转换为voc格式
yii2 中andWhere多个or查询 orm条件
内存泄漏工具VLD安装及使用