I have two classes
public class Parent {
private String name;
private int age;
private ArrayList<Child> children = new ArrayList<Child>();
//Setters and getter to follow..
}
public Class Child {
private String name;
private int age;
}
Spring config includes:
<bean id="jsonMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonMessageConverter" />
</list>
</property>
</bean>
Controller looks like the following:
@RequestMapping(value = "/parents",
method = RequestMethod.POST,
headers="Content-Type=application/json")
public @ResponseBody Parent add(@RequestBody Parent parent, Model model) {
logger.debug("Received request to add a parent");
Parent tempParent = parentService.add(parent); // This will persist the parent object to the DB.. (Helper class)
return tempContract;
}
In a normal circumstances, it should bind the incoming json to Parent and return the Parent as a Json in the response. And it's giving me the exception: "The request sent by the client was syntactically incorrect." with the following input Json:
{
"name" : "foo",
"age" : "45",
"children" : [
{
"name" : "bar",
"age" : "15""
},
{
"name" : "baz",
"age" : "10""
}
]
}
So tried changing the json and it works just fine binding both @RequestBody and @ResponseBody with the following formats:
{
"name" : "foo",
"age" : "45",
"children" : []
}
and
{
"name" : "foo",
"age" : "45",
"children" : [
{}
]
}
So I'm assuming there is something wrong biding the actual child class or with the way I'm passing the Json object wrt Child object. Could someone please help me out here. Also, is it suggested to use
private ArrayList<HashMap<String, Child>> children = new ArrayList<HashMap<String, Child>>();
instead of
private ArrayList<Child> children = new ArrayList<Child>();
Thank you.