解決fastjson反序列化時報錯的問題

weixin_34138377發表於2018-05-03

現在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();

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

相關文章