Jackson Mapper没有反序列化JSON - (无法读取JSON:已经有了id(java.lang.Integer)的POJO)

use*_*124 6 java spring json jackson

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

@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class,
property="@id", scope = CategoryDTO.class)
Run Code Online (Sandbox Code Playgroud)

JSON:

[
   {
      "categories":[
         {
            "@id":27048,
            "name":"Sportbeha's",
            "description":null,
            "parent":{
               "@id":22416,
               "name":"Fitness",
               "description":null,
               "parent":{
                  "@id":21727,
                  "name":"Collectie",
                  "description":null
               }
            }
         },
         {
            "@id":27050,
            "name":"Sportbeha's",
            "description":null,
            "parent":{
               "@id":24474,
               "name":"Voetbal",
               "description":null,
               "parent":21727
            }
         }
      ]
   },
   {
      "categories":[
         {
            "@id":27048,
            "name":"Sportbeha's",
            "description":null,
            "parent":{
               "@id":22416,
               "name":"Fitness",
               "description":null,
               "parent":{
                  "@id":21727,
                  "name":"Collectie",
                  "description":null
               }
            }
         },
         {
            "@id":27050,
            "name":"Sportbeha's",
            "description":null,
            "parent":{
               "@id":24474,
               "name":"Voetbal",
               "description":null,
               "parent":21727
            }
         }
      ]
   }
]
Run Code Online (Sandbox Code Playgroud)

Java代码:

@JsonSerialize(include= JsonSerialize.Inclusion.NON_NULL)
@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id", 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<Set<CategoryDTO>> createBulk(@RequestBody Set<CategoryDTO> categoryDTOs) {

...

}
Run Code Online (Sandbox Code Playgroud)

问题似乎与这片json有关:

"parent":{
    "@id":21727,
    "name":"Collectie",
    "description":null
}
Run Code Online (Sandbox Code Playgroud)

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

Sam*_*rry 1

CategoryDto如果您对每个嵌套对象使用相同的内容,

"parent": 21727
Run Code Online (Sandbox Code Playgroud)

不会反序列化,因为 Jackson 正在等待一个对象。为了反序列化CategoryDto仅具有 id 的父级,您需要 POST 以下 JSON:

"parent": {
    "@id": 21727
}
Run Code Online (Sandbox Code Playgroud)