当前位置:网站首页>Caused by: com.fasterxml.jackson.databind.exc.InvalidFormatException:异常解决
Caused by: com.fasterxml.jackson.databind.exc.InvalidFormatException:异常解决
2022-06-28 05:49:00 【zhou_zhao_xu】
异常信息:
Caused by: com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.util.Date` from String "2022-05-26 15:36:45": not a valid representation (error: Failed to parse Date value '2022-05-26 15:36:45': Cannot parse date "2022-05-26 15:36:45": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSX', parsing fails (leniency? null))
错误原因:
默认情况下,Jackson使用com.fasterxml.jackson.databind.util.StdDateFormat类进行格式化。
时间格式为:yyyy-MM-dd’T’HH:mm:ss.SSSZ,为ISO-8601数据类型。jackson默认的日期反序列不支持yyyy-MM-dd HH:mm:ss 这种格式,所以反序列化失败
解决办法:
- 解决方案一:在yml配置中进行配置序列化格式和时区
spring: jackson: date-format: yyyy-MM-dd HH:mm:ss # 默认是StdDateFormat time-zone: GMT+8
- 解决方案二:在实体类日期参数上使用jackson注解
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8") private Date createTime;
- 解决方案三:在实体类日期参数上使用fastjson注解
<!-- fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.55</version> </dependency> <!-- jackson--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.11.3</version> </dependency>
@JSONField(format = “yyyy-MM-dd HH:mm:ss”) private Date createTime;
- 解决方案四:重写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; 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); } } }
@JsonDeserialize(using = CustomJsonDateDeserializer.class) private Date createTime;
边栏推荐
- Jenkins continuous integration 1
- Typescript interface
- PS effect understanding record 2 color_ dodge color_ burn
- Capacity scheduling absolute value configuration queue usage and pit avoidance
- Line animation
- Yygh-8-appointment registration
- Maskrcnn, fast RCNN, fast RCNN excellent video
- bash install. SH ******** error
- pytorch dataloader的长度 epoch与iteration的区别
- YYGH-BUG-03
猜你喜欢
The windows environment redis uses AOF persistence and cannot generate an AOF file. After generation, the content of the AOF file cannot be loaded
Application of Beidou No.3 short message terminal in dam safety monitoring scheme
Prime mover × Cloud primordial is making sound, reducing cost and increasing efficiency lecture hall
19 fonctions de perte d'apprentissage profond
YYGH-7-用户管理
cocoapod中的第三方库怎么引用本地头文件
阴阳师页面
电子邮件营销的优势在哪里?为什么shopline独立站卖家如此重视?
Interpretation of cloud native microservice technology trend
5G网络整体架构
随机推荐
如何在您的Shopify商店中添加实时聊天功能?
Syn retransmission caused by IPVS
Oracle基础知识总结
CSI以及本地盘的相关实现记录
6. graduation design temperature and humidity monitoring system (esp8266 + DHT11 +oled real-time upload temperature and humidity data to the public network server and display the real-time temperature
pytorch详解
Jenkins continues integration 2
Blog login box
cocoapod中的第三方库怎么引用本地头文件
Data warehouse: detailed explanation of hierarchical design
Windows环境Redis使用AOF持久化,无法生成AOF文件,生成后无法加载AOF文件内容
UICollectionViewDiffableDataSource及NSDiffableDataSourceSnapshot使用介绍
Simple handwritten debounce function
MR-WordCount
【无标题】
[untitled]
Official answers to the "network security" competition questions of the 2022 national vocational college skills competition
How to do a good job of dam safety monitoring
Pre training model parameter mismatch
What are the advantages of e-mail marketing? Why do sellers of shopline independent station attach so much importance to it?