美文网首页
解决fastjson反序列化时报错的问题

解决fastjson反序列化时报错的问题

作者: 爱余星痕 | 来源:发表于2018-05-03 11:11 被阅读0次

    现在JAVA 对象如下:

    @Data
    @NoArgsConstructor
    public class ResourceTreeDto implements Serializable, Comparable<ResourceTreeDto> {
    
       
        private Collection<ResourceTreeDto> children = new TreeSet<>();
    }
    

    该对象需要存进redis里面,序列化进去,没发现问题,但反序列化时,报如下错

    
    com.alibaba.fastjson.JSONException: syntax error, expect {, actual TreeSet, pos 1718, fieldName menuTree, fastjson-version 1.2.39
        at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:451) ~[fastjson-1.2.39.jar:na]
        at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.parseRest(JavaBeanDeserializer.java:1234) ~[fastjson-1.2.39.jar:na]
        at com.alibaba.fastjson.parser.deserializer.FastjsonASMDeserializer_4_ResourceTreeDto.deserialze(Unknown Source) ~[na:na]
        at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:267) ~[fastjson-1.2.39.jar:na]
        at com.alibaba.fastjson.parser.deserializer.DefaultFieldDeserializer.parseField(DefaultFieldDeserializer.java:80) ~[fastjson-1.2.39.jar:na]
        at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.parseField(JavaBeanDeserializer.java:1061) ~[fastjson-1.2.39.jar:na]
        at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:756) ~[fastjson-1.2.39.jar:na]
    

    经查,原来是fastjson不支持泛型,修改原有的对象即可:

    @Data
    @NoArgsConstructor
    public class ResourceTreeDto implements Serializable, Comparable<ResourceTreeDto> {
    
        private static final long serialVersionUID =  
        /**
         * 子节点列表,去掉泛形。
         */
        private Collection  children = new TreeSet();
    
    

    去掉泛形后序列化一起正常。

    相关文章

      网友评论

          本文标题:解决fastjson反序列化时报错的问题

          本文链接:https://www.haomeiwen.com/subject/ojskrftx.html