[英]Is it possible to let Play Framework 1 handle a sent JSON in a post request?
Is it possible to let Play Framework 1 handle a sent JSON in a post request?是否可以让 Play Framework 1 处理发布请求中发送的 JSON? The developer connecting to the backend would not like to send in key/value pairs with an ampersand as separator, he wants to send in a JSON.
连接到后端的开发人员不希望以与号作为分隔符的键/值对发送,他想以 JSON 发送。 Problem is that the data is null, ie
name
is null.问题是数据为空,即
name
为空。 Is this possible to achieve?这有可能实现吗?
What I have so far:到目前为止我所拥有的:
Controller控制器
public static void myMethod(String name) {
Logger.info(name);
}
Routes file路由文件
POST /test Application.myMethod
And I send the request with header Content-Type: application/json
and the data in the body { "name": "A name" }
我发送请求头
Content-Type: application/json
和正文中的数据{ "name": "A name" }
I did not find any auto binding so what I did was this:我没有找到任何自动绑定,所以我所做的是:
MyModel myModel = new GsonBuilder().create().fromJson(new InputStreamReader(request.body), MyModel.class);
And it worked perfectly!它工作得很好!
Actually, I don't know how this worked with you, because it is to give the error: "Stream Closed".实际上,我不知道这如何与您合作,因为它会给出错误:“Stream Closed”。 A workarround to this would be:
对此的解决方法是:
String JSON = request.params.data.get("body")[0];
MyModel myModel = new Gson().fromJson(JSON, MyModel.class);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.