java - Binding the nested json to @RequestBody object using Jackson converter -
java - Binding the nested json to @RequestBody object using Jackson converter -
i have 2 classes
public class parent { private string name; private int age; private arraylist<child> children = new arraylist<child>(); //setters , getter follow.. } public class kid { 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 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 add together parent"); parent tempparent = parentservice.add(parent); // persist parent object db.. (helper class) homecoming tempcontract; } in normal circumstances, should bind incoming json parent , homecoming parent json in response. , it's giving me exception: "the request sent client syntactically incorrect." next input json:
{ "name" : "foo", "age" : "45", "children" : [ { "name" : "bar", "age" : "15"" }, { "name" : "baz", "age" : "10"" } ] } so tried changing json , works fine binding both @requestbody , @responsebody next formats:
{ "name" : "foo", "age" : "45", "children" : [] } and
{ "name" : "foo", "age" : "45", "children" : [ {} ] } so i'm assuming there wrong biding actual kid class or way i'm passing json object wrt kid object. please help me out here. also, suggested use
private arraylist<hashmap<string, child>> children = new arraylist<hashmap<string, child>>(); instead of
private arraylist<child> children = new arraylist<child>(); thank you.
the updated json illustration in question still invalid. recommend checking json examples jsonlint.com.
following illustration of using jackson deserialize corrected version of json same parent/child class construction in current version of question.
import java.util.arraylist; import org.codehaus.jackson.map.objectmapper; public class foo { public static void main(string[] args) throws exception { /* { "name": "foo", "age": "45", "children": [ { "name": "bar", "age": "15" }, { "name": "baz", "age": "10" } ] } */ string jsoninput = "{\"name\":\"foo\",\"age\":\"45\",\"children\":[{\"name\":\"bar\",\"age\":\"15\"},{\"name\":\"baz\",\"age\":\"10\"}]}"; objectmapper mapper = new objectmapper(); parent parent = mapper.readvalue(jsoninput, parent.class); system.out.println(mapper.writevalueasstring(parent)); // output: // {"name":"foo","age":45,"children":[{"name":"bar","age":15},{"name":"baz","age":10}]} } } class parent { public string name; public int age; public arraylist<child> children = new arraylist<child>(); } class kid { public string name; public int age; } is possible message syntax error that: message syntax error? specifically, invalid json input? or real json valid, , examples in question invalid?
"is suggested utilize arraylist<hashmap<string, child>>"
no. json construction doesn't match arraylist<hashmap<string, child>>. so, wouldn't seek utilize it.
java json spring binding jackson
Comments
Post a Comment