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<UserIssueProcessDto> list = JSON.parseObject(json, new TypeReference<List<UserIssueProcessDto>>() {});
        System.out.println(list);

最初发现项目中使用的是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版本以上才能正常解析。

fastjson反序列化LocalDateTime失败的问题java.time.format.DateTimeParseException: Text '2019-05-24 13:52:11' could not be parsed at index 10

原文地址:https://www.cnblogs.com/passedbylove/p/10930665.html

时间: 2024-10-04 23:59:20

fastjson反序列化LocalDateTime失败的问题java.time.format.DateTimeParseException: Text '2019-05-24 13:52:11' could not be parsed at index 10的相关文章

fastjson反序列化多层嵌套泛型类与java中的Type类型

在使用springmvc时,我们通常会定义类似这样的通用类与前端进行交互,以便于前端可以做一些统一的处理: public class Result<T> {   private int ret;   private String msg;   private T data;   // 此处省略getter和setter方法   } 这样的类序列化为json后,js反序列化处理起来毫无压力.但是如果rest接口的消费端就是java呢,java泛型的类型擦除却容易引入一些障碍. 一个反序列化的迭代

fastJson反序列化异常,JSONException: expect &#39;:&#39; at 0, actual =

com.alibaba.fastjson.JSONException: expect ':' at 0, actual = at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:290) ~[fastjson-1.2.47.jar:na] at com.alibaba.fastjson.parser.deserializer.MapDeserializer.deserialze(Ma

java 开发微信中回调验证一直提示 解密失败处理(Java)

微信公众号平台接入JDK6和JDK7及JDK8加解密失败处理(Java) 根据自己jdk版本编译,如jdk7或者jdk6 ,此时部署后提示报错:java.security.InvalidKeyException:illegal Key Size jdk7:微信开发者文档提供了下载JCE无限制权限策略文件UnlimitedJCEPolicyJDK7.zip(JDK7的下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jce-7

42-字符串到json 的错误 com.alibaba.fastjson.JSONObject cannot be cast to java.lang.String

json: {"updated_at":1551780617,"attr":{"uptime_h":3,"uptime_m":17},"did":"GBBxjJYAxE4apkxwEzR3"} Map rMap = (Map) JSON.parse("{"updated_at":1551780617,"attr":{"uptim

《java.util.concurrent 包源码阅读》13 线程池系列之ThreadPoolExecutor 第三部分

这一部分来说说线程池如何进行状态控制,即线程池的开启和关闭. 先来说说线程池的开启,这部分来看ThreadPoolExecutor构造方法: public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecut

Java基础知识强化之集合框架笔记13:Collection集合存储学生对象并遍历

1. Collection集合存储学生对象并遍历: 需求:存储自定义对象并遍历Student(name,age) 分析: (1)创建学生类 (2)创建集合对象 (3)创建学生对象 (4)把学生对象添加到集合对象中 (5)遍历集合 2. 代码示例: Student.java,如下: 1 package cn.itcast_04; 2 3 public class Student { 4 private String name; 5 private int age; 6 7 public Stude

Java 学习笔记(2015.7.20~24)

Java 学习笔记(2015.7.20~24) Java Java 学习笔记(2015.7.20~24) 1.StringBuffer与StringBuilder的区别: 2.基本数据类型包装类 3.Date.Calendar.DateFormat等与时间相关的类 1.常用类String 2.正则表达式 3.常用类 StringBuffer 代表可变的字符序列 拆箱与装箱 包装类的应用 匿名内部类 数组 day11 day12 day13 day14 day11 父类中使用public定义的方法

Java for LeetCode 068 Text Justification

Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified. You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad ex

JAVA String.format 方法使用介绍

1.对整数进行格式化:%[index$][标识][最小宽度]转换方式        我们可以看到,格式化字符串由4部分组成,其中%[index$]的含义我们上面已经讲过,[最小宽度]的含义也很好理解,就是最终该整数转化的字符串最少包含多少位数字.我们来看看剩下2个部分的含义吧: 标识: '-'    在最小宽度内左对齐,不可以与“用0填充”同时使用'#'    只适用于8进制和16进制,8进制时在结果前面增加一个0,16进制时在结果前面增加0x'+'    结果总是包括一个符号(一般情况下只适用