在 Play 2 中使用 json
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10471182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using json with Play 2
提问by mstreffo
I'm trying to create a simple application that allows me to create, read, update and delete various users. I have a basic UI-based view, controller and model that work, but wanted to be more advanced than this and provide a RESTful json interface.
我正在尝试创建一个简单的应用程序,它允许我创建、读取、更新和删除各种用户。我有一个基本的基于 UI 的视图、控制器和模型,但想要比这更先进并提供一个 RESTful json 接口。
However, despite reading everything I can find in the Play 2 documentation, the Play 2 Google groups and the stackoverflow website, I still can't get this to work.
然而,尽管阅读了我可以在 Play 2 文档、Play 2 Google 群组和 stackoverflow 网站中找到的所有内容,我仍然无法让它工作。
I've updated my controller based on previous feedback and I now believe it is based on the documentation.
我已经根据以前的反馈更新了我的控制器,现在我相信它是基于文档的。
Here is my updated controller:
这是我更新的控制器:
package controllers;
import models.Member;
import play.*;
import play.mvc.*;
import play.libs.Json;
import play.data.Form;
public class Api extends Controller {
/* Return member info - version to serve Json response */
public static Result member(Long id){
ObjectNode result = Json.newObject();
Member member = Member.byid(id);
result.put("id", member.id);
result.put("email", member.email);
result.put("name", member.name);
return ok(result);
}
// Create a new body parser of class Json based on the values sent in the POST
@BodyParser.Of(Json.class)
public static Result createMember() {
JsonNode json = request().body().asJson();
// Check that we have a valid email address (that's all we need!)
String email = json.findPath("email").getTextValue();
if(name == null) {
return badRequest("Missing parameter [email]");
} else {
// Use the model's createMember class now
Member.createMember(json);
return ok("Hello " + name);
}
}
....
But when I run this, I get the following error:
但是当我运行它时,我收到以下错误:
incompatible types [found: java.lang.Class<play.libs.Json>] [required: java.lang.Class<?extends play.mvc.BodyParser>]
In /Users/Mark/Development/EclipseWorkspace/ms-loyally/loyally/app/controllers/Api.java at line 42.
41 // Create a new body parser of class Json based on the values sent in the POST
42 @BodyParser.Of(Json.class)
43 public static Result createMember() {
44 JsonNode json = request().body().asJson();
45 // Check that we have a valid email address (that's all we need!)
46 String email = json.findPath("email").getTextValue();
As far as I can tell, I've copied from the documentationso I would appreciate any help in getting this working.
回答by mstreffo
There appear to be conflicts in the use of the Jsonclass in the Play 2 documentation. To get the example above working correctly, the following imports are used:
JsonPlay 2 文档中该类的使用似乎存在冲突。为了使上面的示例正常工作,使用了以下导入:
import play.mvc.Controller;
import play.mvc.Result;
import play.mvc.BodyParser;
import play.libs.Json;
import play.libs.Json.*;
import static play.libs.Json.toJson;
import org.codehaus.Hymanson.JsonNode;
import org.codehaus.Hymanson.node.ObjectNode;
@BodyParser.Of(play.mvc.BodyParser.Json.class)
public static index sayHello() {
JsonNode json = request().body().asJson();
ObjectNode result = Json.newObject();
String name = json.findPath("name").getTextValue();
if(name == null) {
result.put("status", "KO");
result.put("message", "Missing parameter [name]");
return badRequest(result);
} else {
result.put("status", "OK");
result.put("message", "Hello " + name);
return ok(result);
}
}
Note the explicit calling of the right Jsonclass in @BodyParser
注意正确的Json类的显式调用@BodyParser
I'm not sure if this is a bug or not? But this is the only way I could get the example to work.
我不确定这是否是一个错误?但这是我让示例工作的唯一方法。
回答by greg
Import those two
导入这两个
import com.fasterxml.Hymanson.databind.JsonNode;
import com.fasterxml.Hymanson.databind.node.ObjectNode;
According to this documentation: http://fasterxml.github.io/Hymanson-databind/javadoc/2.0.0/com/fasterxml/Hymanson/databind/node/ObjectNode.html
根据此文档:http: //fasterxml.github.io/Hymanson-databind/javadoc/2.0.0/com/fasterxml/Hymanson/databind/node/ObjectNode.html
回答by Tam Mai
Try this:
尝试这个:
import play.*;
import play.mvc.*;
import org.codehaus.Hymanson.JsonNode; //Fixing "error: cannot find symbol" for JsonNode
// Testing JSON
@BodyParser.Of(BodyParser.Json.class) //Or you can import play.mvc.BodyParser.Json
public static Result sayHello() {
JsonNode json = request().body().asJson();
String name = json.findPath("name").getTextValue();
if(name==null) {
return badRequest("Missing parameter [name]");
} else {
return ok("Hello " + name);
}
}
回答by ndeverge
AFAIK, the code you are using has not reached any official Play version (neither 2.0 or 2.0.1) according to this: https://github.com/playframework/Play20/pull/212
AFAIK,您使用的代码尚未达到任何官方 Play 版本(2.0 或 2.0.1),根据此:https: //github.com/playframework/Play20/pull/212
Instead, you can do this (not tested):
相反,您可以这样做(未测试):
if(request().getHeader(play.mvc.Http.HeaderNames.ACCEPT).equalsIgnoreCase("application/json")) {
回答by Anthony
Did you try checking out the documentationfor it?
您是否尝试查看它的文档?
Serving a JSON response looks like:
提供 JSON 响应如下所示:
@BodyParser.Of(Json.class)
public static index sayHello() {
JsonNode json = request().body().asJson();
ObjectNode result = Json.newObject();
String name = json.findPath("name").getTextValue();
if(name == null) {
result.put("status", "KO");
result.put("message", "Missing parameter [name]");
return badRequest(result);
} else {
result.put("status", "OK");
result.put("message", "Hello " + name);
return ok(result);
}
}
回答by Victor
You have imported play.libs.Jsonand then use the BodyParser.Ofannotation with this Json.class.
您已导入play.libs.Json并使用BodyParser.Of带有 this的注释Json.class。
The above annotation expects a class which extends a play.mvc.BodyParser. So simply replace @BodyParser.Of(Json.class)by @BodyParser.Of(BodyParser.Json.class).
上面的注释需要一个扩展 a 的类play.mvc.BodyParser。所以简单地替换@BodyParser.Of(Json.class)为@BodyParser.Of(BodyParser.Json.class).

