Reputation: 8850
Client code:
$.ajax({
type: "POST",
url: "../web/zittles",
data: jsonformatdata,
contentType: "application/json",
dataType: "json",
success: function(data)
{
alert("data from server : "+data);
},
error: function(jqXHR, textStatus, errorThrown)
{
alert("jqXHR.status = "+jqXHR.status); //getting status code 400 here
}
});
Output json data:
{
"id": 1,
"No": "1234",
"Desc": "Testing"
}
Java class:
public class Fizzle implements Serializable
{
private String id;
private String No;
private String Desc;
// getters and setters
}
Spring 3 Controller:
@RequestMapping(value = '/zittles', method = RequestMethod.POST, headers ="Content-Type=application/json")
public @ResponseBody void doSomeThing (@RequestBody Fizzle fizzle) {
//do something here
}
app-servlet.xml has
<mvc:annotation-driven/>
/lib folder of tomcat has
jackson-core-lgpl-1.9.10.jar
jackson-mapper-lgpl-1.9.10.jar
Getting error with status code 400 -
"The request sent by the client was syntactically incorrect"
When I change the Controller code as shown below, it takes the json data as string.
public @ResponseBody void doSomeThing (@RequestBody String fizzle) {}
Ideally Jackson should automatically map the json data to Fizzle object.
What is it that I am missing here. Is there anything else that has to be done to configure the Jackson parser correctly?
Help please.
Upvotes: 2
Views: 6959
Reputation: 8850
I am answering my own question since it worked for me. Thanks "vacuum" for giving me the direction. I am using List since I am receving an array of json data.
@JsonTypeName
@RequestMapping(value = '/zittles', method = RequestMethod.POST, headers ="Content-Type=application/json")
public @ResponseBody void doSomeThing (@RequestBody String fizzles)
{
//do something here
ObjectMapper mapper = new ObjectMapper();
ArrayList list = mapper.readValue(new StringReader(fizzles), ArrayList.class);
List<Fizzle> fizzles1 = mapper.convertValue(list, new TypeReference<List<Fizzle>>() {});
}
Upvotes: 0
Reputation: 2273
Id must be long or int, not a String
private int id;
Edit
Maybe try this JSON:
{
"id": 1,
"no": "1234",
"desc": "Testing"
}
and change properties to lowercase in Fizzle class
Edit2 Test if this is Jakson issue or Spring issue. Save your JSON to file and try to convert it to object:
ObjectMapper mapper = new ObjectMapper();
Fizzle fizzle = mapper.readValue(new File("c:\\fizzle.json"), Fizzle .class);
Upvotes: 2