I want to send a Json with this format
{
"tarjetas": [
{"nombre":nombre, "id": id, "lista": lista, "idLista": idLista},
{"nombre":nombre, "id": id, "lista": lista, "idLista": idLista}
...
]
}
I build it like this:
var tarjetas = [];
var objeto = {};
tarjetas.push({
"nombre" : arrayCards[i],
"id" : arrayCardsId[i],
"nombreLista" : nombreLista,
"idLista": arrayCardsList[i]
});
objeto.tarjetas = tarjetas;
For this I have this code in Javascript with jQuery:
$.ajax({
type: "POST",
contentType : 'application/json; charset=utf-8',
dataType : 'json',
url: "http://localhost:8080/HelloSpringMVC/alo",
data: JSON.stringify(objeto),
success :function(result) {
// do what ever you want with data
}
Controller code:
@RequestMapping(value= "/j", method = RequestMethod.POST)
public String recibe(@RequestBody Tarjetas tarjeta){
System.out.println("Post");
System.out.println(tarjeta.toString());
return "index";
}
Class Cards:
public class Tarjetas {
private String tName;
private String tId;
private String nameList;
private String idList;
}
My problem is that I don't understand how to receive the Json in the controller, to work later with the received data, thanks.
Your
json
input should have a direct mapping to the class you receive with@RequestBody
Given:
You must define a class with an attribute
tarjetas
that is a list (because of the array in thejson
):Each item in the list
tarjetas
has an objectjson
:So in java the
List tarjetas
must be from a class that has the attributes:nombre
,id
,lista
,idLista
: