Spring   发布时间:2022-04-09  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Jackson Mapper没有反序列化JSON – (无法读取JSON:已经有了id(java.lang.Integer)的POJO)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

在将json发布到Spring Controller时获得上述异常.看来Jackson Mapper无法反序列化json. CategoryDTO注释为:

@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class,property="@id",scope = CategoryDTO.class)

JSON:

[
   {
      "categories":[
         {
            "@id":27048,"name":"Sportbeha's","description":null,"parent":{
               "@id":22416,"name":"Fitness","parent":{
                  "@id":21727,"name":"Collectie","description":null
               }
            }
         },{
            "@id":27050,"parent":{
               "@id":24474,"name":"Voetbal","parent":21727
            }
         }
      ]
   },{
      "categories":[
         {
            "@id":27048,"parent":21727
            }
         }
      ]
   }
]

Java代码

@Jsonserialize(include= Jsonserialize.Inclusion.NON_NULL)
@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class,scope = CategoryDTO.class)
@JsonIgnoreProperties(ignoreUnkNown = truE)
public class CategoryDTO implements serializable{

    private Long id;
    private String name;
    private String description;
    private CategoryDTO parent;

    @JsonIgnore
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String Name) {
        this.name = name;
    }


    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public CategoryDTO getParent() {
        return parent;
    }

    public void setParent(CategoryDTO parent) {
        this.parent = parent;
    }

}


**Spring Controller :** 

    @requestMapping(value = "/categories",method = requestMethod.POST,consumes =  "application/json;charset=UTF-8",produces = "application/json;charset=UTF-8")
    public ResponseEntity<>@H_450_24@

问题似乎与这片json有关:

"parent":{
    "@id":21727,"description":null
}

它存在于数组中的两个对象中.

最佳答案
如果对每个嵌套对象使用相同的CategoryDto,

"parent": 21727

因为杰克逊期待一个对象,所以不会反序列化.要仅使用id反序列化父CategoryDto,您需要POST以下JSON:

"parent": {
    "@id": 21727
}

大佬总结

以上是大佬教程为你收集整理的Jackson Mapper没有反序列化JSON – (无法读取JSON:已经有了id(java.lang.Integer)的POJO)全部内容,希望文章能够帮你解决Jackson Mapper没有反序列化JSON – (无法读取JSON:已经有了id(java.lang.Integer)的POJO)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。