rest - 400 Bad Request when posting JSON Array to a Spring controller -
i have been successful in sending simple json object on spring framework controller
curl -v -h "content-type: application/json" -h "accept: application/json" -d '{"lastname":"smith","firstname":"john"}' http://localhost:8080/webservices02/aura/testjsonarray
however, when send on json object embedded array
curl -v -h "content-type: application/json" -h "accept: application/json" -d '{"lastname":"smith","pals":[{"name":"billy"}],"firstname":"john"}' http://localhost:8080/webservices02/aura/testjsonarray
i error 400 'the request sent client syntactically incorrect ().'
my controller
@controller @requestmapping(value="/aura") public class auracontroller { @requestmapping(value = "/testjsonarray", method = requestmethod.post, headers = {"content-type=application/json"}) @responsebody public void testjsonarray(@requestbody aura aura){ system.err.println("called testjsonarray(): " + "aura " + aura.tostring()); }
}
my aura class defined
import java.io.serializable; import java.util.list; import org.json.jsonobject; public class aura implements serializable { public class pal{ private string name; public string getname() { return name; } public void setname(string name) { this.name = name; } } private string firstname; private string lastname; public string getfirstname() { return firstname; } public void setfirstname(string firstname) { this.firstname = firstname; } public string getlastname() { return lastname; } public void setlastname(string lastname) { this.lastname = lastname; } private list<pal> pals; public string tostring(){ return new jsonobject(this).tostring(); } public list<pal> getpals() { return pals; } public void setpals(list<pal> pals) { this.pals = pals; }
}
my bean (under mvc:message-converters) is
<bean class="org.springframework.http.converter.json.mappingjacksonhttpmessageconverter"/>
if can me on this... thanks
you have include jackson json mapper dependency jackson-core-asl , jackson-mapper-asl in project.
this necessary mapping json java pojo class fields.
Comments
Post a Comment