本地java類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; } }
測試代碼spring
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<UserIssueProcessDto> list = JSON.parseObject(json, new TypeReference<List<UserIssueProcessDto>>() {}); System.out.println(list);
最初發現項目中使用的是fastjson-1.2.7.jarjson
報錯以下:mybatis
com.alibaba.fastjson.JSONException: Text '2019-05-24 13:52:11' could not be parsed at index 10mvc
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配置無效。this
後來聯想到最近使用的mybatis使用LocalDateTime/LocalDate等類使用了jsr310的時間類,須要額外添加jar包支持。lua
最初錯誤的把jackson-datatype-jsr310-2.7.0-rc1.jar添加進來,調試一直無效。spa
後來發現一篇文章Fastjson支持Java8中的LocalDateTime嗎?,說fastjson-1.2.11支持,開始更換版本。.net
筆者從mvnrepository.com下載相應的版本,通過最終測試,wenshao的說法是錯誤的。
必須使用fastjson-1.2.12版本以上才能正常解析。