2014-07-04 14 views
6

Ottenere la suddetta eccezione durante la pubblicazione di un json su Spring Controller. Sembra che Jackson Mapper non sia in grado di deserializzare il json. CategoryDTO è annotata con:Jackson Mapper Not Deserializing JSON - (Impossibile leggere JSON: già POJO per id (java.lang.Integer))

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

Java CODICE:

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

... 

} 

Il problema sembra essere con questo pezzo di JSON:

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

Che esiste in entrambi gli oggetti nell'array.

+0

Due oggetti nella serie principale sono gli stessi. Va bene? –

risposta

1

Se si utilizza lo stesso CategoryDto per ciascuno dei vostri oggetti nidificati,

"parent": 21727 

non si deserializzare dal Jackson è in attesa di un oggetto. Al fine di deserializzare un genitore CategoryDto solo con un id sarà necessario pubblicare il seguente JSON invece:

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