2014-07-04 7 views
6

वसंत नियंत्रक के लिए एक जेसन पोस्ट करते समय उपरोक्त अपवाद प्राप्त करना। ऐसा लगता है जैक्सन मैपर जेसन को बेकार करने में सक्षम नहीं है। CategoryDTO साथ टिप्पणी की जाती है:जैक्सन मैपर जेएसओएन को Deserializing नहीं - (JSON नहीं पढ़ सका: आईडी (java.lang.Integer) के लिए पहले से ही POJO था)

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

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 
      } 
     } 
     ] 
    } 
] 

जावा कोड:

@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) { 

... 

} 

समस्या json के इस टुकड़े के साथ हो रहा है:

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

जो सरणी में दोनों वस्तुओं में मौजूद है। के बाद से जैक्सन एक वस्तु की उम्मीद है

+0

दो जड़ सरणी में वस्तुओं एक ही हैं। ठीक है न? –

उत्तर

1

आप अपने नेस्टेड वस्तुओं में से प्रत्येक के लिए एक ही CategoryDto का उपयोग कर रहे हैं, तो

"parent": 21727 

deserialize नहीं होंगे। आदेश केवल एक आईडी के साथ एक माता पिता CategoryDto deserialize करने के लिए आप के बजाय निम्नलिखित JSON पोस्ट करने की आवश्यकता होगी:

"parent": { 
    "@id": 21727 
} 
संबंधित मुद्दे