当前位置:网站首页>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>
边栏推荐
- 别再用 System.currentTimeMillis() 统计耗时了,太 Low,StopWatch 好用到爆!
- [combinatorics] Application of exponential generating function (multiple set arrangement problem | different balls in different boxes | derivation of exponential generating function of odd / even sequ
- Opengauss database development and debugging tool guide
- C language beginner level - pointer explanation - paoding jieniu chapter
- 用docker 連接mysql的過程
- 从C到Capable-----利用指针作为函数参数求字符串是否为回文字符
- Do you really understand relays?
- [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
- MySql實戰45講【SQL查詢和更新執行流程】
- The difference between componentscan and componentscans
猜你喜欢
![[principles of multithreading and high concurrency: 1_cpu multi-level cache model]](/img/c7/6b5ab4ff7379bfccff7cdbb358ff8f.jpg)
[principles of multithreading and high concurrency: 1_cpu multi-level cache model]

Sqlserver row to column pivot

I2C 子系统(一):I2C spec

Add automatic model generation function to hade

Deep reinforcement learning for intelligent transportation systems: a survey paper reading notes

力扣------网格中的最小路径代价

MySql实战45讲【全局锁和表锁】

Use of El tree search method

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

I2C subsystem (I): I2C spec
随机推荐
Vs 2019 configuration du moteur de génération de tensorrt
用docker 連接mysql的過程
为什么线程崩溃不会导致 JVM 崩溃
labelimg生成的xml文件转换为voc格式
Hi3536C V100R001C02SPC040 交叉编译器安装
Find the storage address of the elements in the two-dimensional array
The difference between componentscan and componentscans
Parameter index out of range (1 > number of parameters, which is 0)
Distributed transaction
Force freeing memory in PHP
MySQL practice 45 lecture [transaction isolation]
解决高並發下System.currentTimeMillis卡頓
MySQL practice 45 [SQL query and update execution process]
How do you adjust the scope of activerecord Association in rails 3- How do you scope ActiveRecord associations in Rails 3?
QT based tensorrt accelerated yolov5
From C to capable -- use the pointer as a function parameter to find out whether the string is a palindrome character
Add some hard dishes to the interview: how to improve throughput and timeliness in delayed task scenarios!
I2C 子系统(一):I2C spec
Deep learning: multi-layer perceptron and XOR problem (pytoch Implementation)
JS finds all the parent nodes or child nodes under a node according to the tree structure