当前位置:网站首页>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>
边栏推荐
- How to limit the size of the dictionary- How to limit the size of a dictionary?
- Sqlserver row to column pivot
- Super easy to use logzero
- Use optimization | points that can be optimized in recyclerview
- MySql实战45讲【索引】
- VS 2019安装及配置opencv
- [C language] MD5 encryption for account password
- 当lambda没有输入时,是何含义?
- Basic information of Promethus (I)
- How to make backgroundworker return an object
猜你喜欢
Opengauss database development and debugging tool guide
VS 2019配置tensorRT
Use of El tree search method
Add automatic model generation function to hade
MySql實戰45講【SQL查詢和更新執行流程】
docker安装redis
Privatization lightweight continuous integration deployment scheme -- 01 environment configuration (Part 2)
I2C subsystem (II): I3C spec
Three. JS local environment setup
Super easy to use logzero
随机推荐
复选框的使用:全选,全不选,选一部分
Basic information of Promethus (I)
el-tree搜索方法使用
Getting started | jetpack hilt dependency injection framework
MySql实战45讲【SQL查询和更新执行流程】
TCP 三次握手和四次挥手机制,TCP为什么要三次握手和四次挥手,TCP 连接建立失败处理机制
左连接,内连接
Concrete CMS vulnerability
敏捷认证(Professional Scrum Master)模拟练习题-2
SqlServer行转列PIVOT
用docker 連接mysql的過程
C language beginner level - pointer explanation - paoding jieniu chapter
Counter统计数量后,如何返回有序的key
[shutter] monitor the transparency gradient of the scrolling action control component (remove the blank of the top status bar | frame layout component | transparency component | monitor the scrolling
模糊查询时报错Parameter index out of range (1 > number of parameters, which is 0)
How do you adjust the scope of activerecord Association in rails 3- How do you scope ActiveRecord associations in Rails 3?
How to return ordered keys after counter counts the quantity
Docker install redis
Installation and use of memory leak tool VLD
I2C 子系统(四):I2C debug