restful POST in Play
I have been following some Play! framework tutorials, but I cannot get the simplest of examples to work.
String xml = "<thing><foo>first</foo><bar>second</bar&开发者_如何学编程gt;</thing>";
Response response = POST("/thing", "application/xml", xml);
In my /thing
method in the controller, the Thing
object that is passed in is always null
.
Does anyone have a code snippet that successfully POSTs a xml document to a controller action, and then ends up with a deserialised object at the end that can be persisted to the database?
It seems that there is an error in the book. The code Request.current().params.get("body")
in the method ApiPlugin.getXml(Class)
returns String
object. There is no overloaded method unmarshal(String)
in the interface javax.xml.bind.Unmarshaller
. To make it works wrap that string into java.io.StringReader
:
String body = Request.current().params.get("body");
return um.unmarshal(new StringReader(body));
精彩评论