我正在尝试创建一个简单的应用程序,允许我创建、读取、更新和删除各种用户。我有一个基本的基于 UI 的视图、控制器和模型,但我想比这更高级,并提供一个 RESTful json 接口。
但是,尽管阅读了我在 Play 2 文档、Play 2 Google 组和 stackoverflow 网站中可以找到的所有内容,但我仍然无法使其正常工作。
我已经根据之前的反馈更新了我的控制器,现在我相信它是基于文档的。
这是我更新的控制器:
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);
}
}
....
但是当我运行它时,我收到以下错误:
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();
据我所知,我已经从文档中复制了,因此我将不胜感激任何帮助使其正常工作。