vendredi 8 mai 2015

Can Spring MVC deserialize JSON which can be object or array?

I have controller receiving JSON in request body, which can be object or array of objects. For example:

{
  "id" : 1,
  "name" : "Nick",
  "surname" : "Cave"
}

and

[
 {
   "id" : 1,
   "name" : "Nick",
   "surname" : "Cave"
 },
 {
   "id" : 2,
   "name" : "Jack",
   "surname" : "White"
 }
]

Is there any way to force Spring to deserialize JSON to object in the similar way to object alone?

@RequestMapping(value = "/", method = RequestMethod.POST)
public void postController(@RequestBody User user, ...) {
   ...
}

If not, what is the elegant way of parsing and validating those kind of messages?

Aucun commentaire:

Enregistrer un commentaire