Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson Mapper Not Deserializing JSON - (Could not read JSON: Already had POJO for id (java.lang.Integer))

Getting the above said exception while posting a json to Spring Controller. It seems Jackson Mapper is not able to deserialize the json. CategoryDTO is annotated with:

@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 CODE :

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

...

}

The problem seems to be with this piece of json:

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

Which exists in both the objects in the array.

like image 918
user3804124 Avatar asked Jul 04 '14 05:07

user3804124


1 Answers

If you are using the same CategoryDto for each of your nested objects,

"parent": 21727

will not deserialize since Jackson is expecting an object. In order to deserialize a parent CategoryDto with only an id you will need to POST the following JSON instead:

"parent": {
    "@id": 21727
}
like image 151
Sam Berry Avatar answered Oct 23 '22 12:10

Sam Berry