当前位置:网站首页>com.fasterxml.jackson.databind.exc.InvalidFormatException问题
com.fasterxml.jackson.databind.exc.InvalidFormatException问题
2022-07-03 03:08:00 【&友情岁月&】
客户端使用fastjson进行序列化,服务端使用jackson反序列化失败。报错。
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))
原因:
fastjson默认使用的序列化格式:
public static String DEFFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
jackson默认支持的日期反序列格式:
("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默认的日期反序列不支持yyyy-MM-dd HH:mm:ss 这种格式,所以反序列化失败
解决办法一:重写jackson反序列化
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 重写反序列化
* @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);
}
}
}
在需要的date属性上添加注解:
@JsonDeserialize(using = CustomJsonDateDeserializer.class)
private Date createTime;
使用这种方式,需要重写上面反序列化抽象类
解决办法二:直接使用jackson注解,不需要重写反序列化
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date createTime;
GMT+8表示时区,东八区
解决办法三:直接使用fastjson注解,不要重写反序列化
@JSONField(format = “yyyy-MM-dd HH:mm:ss”)
private Date createTime;
添加依赖:
<!-- 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
- The solution of "the required function is not supported" in win10 remote desktop connection is to modify the Registry [easy to understand]
- Check log4j problems using stain analysis
- ComponentScan和ComponentScans的区别
- Kubernetes cluster log and efk architecture log scheme
- Cron表达式介绍
- Segmentation fault occurs during VFORK execution
- open file in 'w' mode: IOError: [Errno 2] No such file or directory
- Introduction to cron expression
- Getting started | jetpack hilt dependency injection framework
猜你喜欢

Privatization lightweight continuous integration deployment scheme -- 01 environment configuration (Part 2)

Agile certification (professional scrum Master) simulation exercise-2

I2C 子系統(四):I2C debug

I2C subsystem (III): I2C driver

Check log4j problems using stain analysis

Spark on yarn resource optimization ideas notes

SqlServer行转列PIVOT

Idea format code idea set shortcut key format code

Spark on yarn资源优化思路笔记

Do you really understand relays?
随机推荐
模糊查詢時報錯Parameter index out of range (1 > number of parameters, which is 0)
vfork执行时出现Segmentation fault
The idea cannot be loaded, and the market solution can be applied (pro test)
Edit and preview in the back pipe to get the value writing method of the form
Source code analysis | resource loading resources
The process of connecting MySQL with docker
[C语言]给账号密码进行MD5加密
Find the storage address of the elements in the two-dimensional array
【PyG】理解MessagePassing过程,GCN demo详解
Basic information of Promethus (I)
基于Qt的yolov5工程
模糊查询时报错Parameter index out of range (1 > number of parameters, which is 0)
迅雷chrome扩展插件造成服务器返回的数据js解析页面数据异常
I2C 子系統(四):I2C debug
Notifydatasetchanged not applicable to recyclerview - notifydatasetchanged not working on recyclerview
VS 2019配置tensorRT
VS code配置虚拟环境
力扣------网格中的最小路径代价
The left value and the right finger explain better
销毁Session和清空指定的属性