当前位置:网站首页>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>
边栏推荐
- Notifydatasetchanged not applicable to recyclerview - notifydatasetchanged not working on recyclerview
- MySQL practice 45 [SQL query and update execution process]
- Edit and preview in the back pipe to get the value writing method of the form
- js根据树结构查找某个节点的下面的所有父节点或者子节点
- Use optimization | points that can be optimized in recyclerview
- How to return ordered keys after counter counts the quantity
- Pytorch配置
- Agile certification (professional scrum Master) simulation exercises
- The idea setting code is in UTF-8 idea Properties configuration file Chinese garbled
- 3D drawing example
猜你喜欢

From C to capable -- use the pointer as a function parameter to find out whether the string is a palindrome character

MySql实战45讲【行锁】

Deep learning: multi-layer perceptron and XOR problem (pytoch Implementation)

敏捷认证(Professional Scrum Master)模拟练习题-2

VS code配置虚拟环境

Agile certification (professional scrum Master) simulation exercise-2

你真的懂继电器吗?

Check log4j problems using stain analysis
![[Chongqing Guangdong education] cultural and natural heritage reference materials of China University of Geosciences (Wuhan)](/img/19/815e7cba81f6eb52db5ef0db556dfd.jpg)
[Chongqing Guangdong education] cultural and natural heritage reference materials of China University of Geosciences (Wuhan)

I2C 子系统(三):I2C Driver
随机推荐
el-tree搜索方法使用
文件重命名
I2C 子系统(二):I3C spec
C#通用接口调用
Pytest (6) -fixture (Firmware)
MySql實戰45講【SQL查詢和更新執行流程】
labelimg生成的xml文件转换为voc格式
ASP. Net core 6 framework unveiling example demonstration [02]: application development based on routing, MVC and grpc
I2C 子系统(三):I2C Driver
Spark on yarn resource optimization ideas notes
Thunderbolt Chrome extension caused the data returned by the server JS parsing page data exception
PAT乙级“1104 天长地久”DFS优化思路
C # general interface call
Introduction to cron expression
Idea format code idea set shortcut key format code
I2C 子系统(四):I2C debug
Are there any recommended term life insurance products? I want to buy a term life insurance.
Deep learning: multi-layer perceptron and XOR problem (pytoch Implementation)
[algebraic structure] group (definition of group | basic properties of group | proof method of group | commutative group)
Installation and use of memory leak tool VLD