fastjson反序列化LocalDateTime失败的问题java.time.format.DateTimeParseException: Text '2019-05-24 13:52:11' could not be parsed at index 10
本地java类
import org.springframework.format.annotation.DateTimeFormat;
import java.time.LocalDateTime;
/***
-
问题处理记录
*/
public class UserIssueProcessDto {
/***
- 处理说明
/
private String f_clsm;
/**
- 施工队处理反馈
*/
private String f_fk;
/***
- 处理开始时间
*/
@DateTimeFormat( pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime f_clkssj;
/***
- 处理结束时间
*/
@DateTimeFormat( pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime f_cljssj;
/***
- 处理时长
*/
private Double duration;
private String f_clry;
private String f_clryid;
/***
- 填写人ID
/
private Integer creatorId;
/**
- 填写人名称
*/
private String creatorName;
/***
- 添加时间
*/
private LocalDateTime f_addtime;
public String getF_clsm() {
return f_clsm;
}
public void setF_clsm(String f_clsm) {
this.f_clsm = f_clsm;
}
public String getF_fk() {
return f_fk;
}
public void setF_fk(String f_fk) {
this.f_fk = f_fk;
}
public LocalDateTime getF_clkssj() {
return f_clkssj;
}
public void setF_clkssj(LocalDateTime f_clkssj) {
this.f_clkssj = f_clkssj;
}
public LocalDateTime getF_cljssj() {
return f_cljssj;
}
public void setF_cljssj(LocalDateTime f_cljssj) {
this.f_cljssj = f_cljssj;
}
public Double getDuration() {
return duration;
}
public void setDuration(Double duration) {
this.duration = duration;
}
public String getF_clry() {
return f_clry;
}
public void setF_clry(String f_clry) {
this.f_clry = f_clry;
}
public String getF_clryid() {
return f_clryid;
}
public void setF_clryid(String f_clryid) {
this.f_clryid = f_clryid;
}
public Integer getCreatorId() {
return creatorId;
}
public void setCreatorId(Integer creatorId) {
this.creatorId = creatorId;
}
public String getCreatorName() {
return creatorName;
}
public void setCreatorName(String creatorName) {
this.creatorName = creatorName;
}
public LocalDateTime getF_addtime() {
return f_addtime;
}
public void setF_addtime(LocalDateTime f_addtime) {
this.f_addtime = f_addtime;
}
}
测试代码
String json = "[{\"f_clsm\":\"111\",\"f_fk\":\"-1\",\"f_clkssj\":\"2019-05-24 13:52:11\",\"f_cljssj\":\"2019-05-24 14:52:11\",\"f_clryid\":\"1000645\"}]";
List</span><UserIssueProcessDto> list = JSON.parseObject(json, <span style="color: #0000ff;">new</span> TypeReference<List<UserIssueProcessDto>><span style="color: #000000;">() {});
System.out.println(list);</span></pre>
最初发现项目中使用的是fastjson-1.2.7.jar
报错如下:
com.alibaba.fastjson.JSONException: Text '2019-05-24 13:52:11' could not be parsed at index 10
at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:555)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:251)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:227)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:181)
at com.TestSpringMVCConvert.test(TestSpringMVCConvert.java:19)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.time.format.DateTimeParseException: Text '2019-05-24 13:52:11' could not be parsed at index 10
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.time.LocalDateTime.parse(LocalDateTime.java:492)
at java.time.LocalDateTime.parse(LocalDateTime.java:477)
at com.alibaba.fastjson.parser.deserializer.Jdk8DateCodec.deserialze(Jdk8DateCodec.java:35)
at com.alibaba.fastjson.parser.deserializer.DefaultFieldDeserializer.parseField(DefaultFieldDeserializer.java:33)
at com.alibaba.fastjson.parser.deserializer.ASMJavaBeanDeserializer.parseField(ASMJavaBeanDeserializer.java:74)
at com.alibaba.fastjson.parser.deserializer.ASMJavaBeanDeserializer$InnerJavaBeanDeserializer.parseField(ASMJavaBeanDeserializer.java:86)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:330)
at com.alibaba.fastjson.parser.deserializer.ASMJavaBeanDeserializer.parseRest(ASMJavaBeanDeserializer.java:100)
at Fastjson_ASM_UserIssueProcessDto_1.deserialze(Unknown Source)
at com.alibaba.fastjson.parser.DefaultJSONParser.parseArray(DefaultJSONParser.java:635)
at com.alibaba.fastjson.parser.deserializer.CollectionDeserializer.deserialze(CollectionDeserializer.java:63)
at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:551)
... 26 more
最早发现这个问题是在springmvc实体属性注入的时候,经过修改各种springmvc配置无效。
后来联想到最近使用的mybatis使用LocalDateTime/LocalDate等类使用了jsr310的时间类,需要额外添加jar包支持。
最初错误的把jackson-datatype-jsr310-2.7.0-rc1.jar添加进来,调试一直无效。
后来发现一篇文章Fastjson支持Java8中的LocalDateTime吗?,说fastjson-1.2.11支持,开始更换版本。
笔者从mvnrepository.com下载相应的版本,经过最终测试,wenshao的说法是错误的。
必须使用fastjson-1.2.12版本以上才能正常解析。
原文地址:https://www.cnblogs.com/passedbylove/p/10930665.html