当前位置:网站首页>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>
边栏推荐
- Process the dataset and use labelencoder to convert all IDs to start from 0
- Gavin teacher's perception of transformer live class - rasa project's actual banking financial BOT Intelligent Business Dialogue robot architecture, process and phenomenon decryption through rasa inte
- [C language] MD5 encryption for account password
- @Accessors注解作用指定前缀遵守驼峰命名
- [C语言]给账号密码进行MD5加密
- Docker install redis
- 别再用 System.currentTimeMillis() 统计耗时了,太 Low,StopWatch 好用到爆!
- MySQL practice 45 [global lock and table lock]
- ASP. Net core 6 framework unveiling example demonstration [02]: application development based on routing, MVC and grpc
- docker安装redis
猜你喜欢
[pyg] understand the messagepassing process, GCN demo details
Force deduction ----- the minimum path cost in the grid
Thunderbolt Chrome extension caused the data returned by the server JS parsing page data exception
TCP 三次握手和四次挥手机制,TCP为什么要三次握手和四次挥手,TCP 连接建立失败处理机制
MySql实战45讲【SQL查询和更新执行流程】
VS 2019 配置tensorRT生成engine
el-tree搜索方法使用
Deep learning: multi-layer perceptron and XOR problem (pytoch Implementation)
Three.js本地环境搭建
Nasvit: neural architecture search of efficient visual converter with gradient conflict perception hypernetwork training
随机推荐
MySql實戰45講【SQL查詢和更新執行流程】
文件重命名
Sqlserver row to column pivot
Tensorflow to pytorch notes; tf. gather_ Nd (x, y) to pytorch
ASP. Net core 6 framework unveiling example demonstration [02]: application development based on routing, MVC and grpc
Chart. JS multitooltip tag - chart js multiTooltip labels
[algebraic structure] group (definition of group | basic properties of group | proof method of group | commutative group)
PAT乙级常用函数用法总结
Hi3536C V100R001C02SPC040 交叉编译器安装
How to limit the size of the dictionary- How to limit the size of a dictionary?
用docker 连接mysql的过程
后管中编辑与预览获取表单的值写法
[C语言]给账号密码进行MD5加密
C # general interface call
销毁Session和清空指定的属性
解决高并发下System.currentTimeMillis卡顿
Creation and destruction of function stack frame
[leectode 2022.2.15] lucky numbers in the matrix
敏捷认证(Professional Scrum Master)模拟练习题-2
TCP handshake three times and wave four times. Why does TCP need handshake three times and wave four times? TCP connection establishes a failure processing mechanism