在Play 2中使用json [英] Using json with Play 2

查看:100
本文介绍了在Play 2中使用json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个简单的应用程序,该应用程序允许我创建,读取,更新和删除各种用户.我有一个可以使用的基于UI的基本视图,控制器和模型,但是我想比这更高级,并提供一个RESTful json接口.

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.

但是,尽管阅读了我在Play 2文档,Play 2 Google小组和stackoverflow网站上都能找到的所有内容,但我仍然无法使用它.

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.

我已经根据以前的反馈更新了控制器,现在我相信它是基于文档的.

I've updated my controller based on previous feedback and I now believe it is based on the documentation.

这是我更新的控制器:

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 documentation so I would appreciate any help in getting this working.

推荐答案

在Play 2文档中,使用Json类似乎存在冲突.为了使上面的示例正常工作,使用了以下导入:

There appear to be conflicts in the use of the Json class in the Play 2 documentation. To get the example above working correctly, the following imports are used:

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.jackson.JsonNode;           
import org.codehaus.jackson.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);
    }
}

请注意在@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.

这篇关于在Play 2中使用json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆