2012-05-06 4 views
4

私はさまざまなユーザーの作成、読み取り、更新、削除を可能にする簡単なアプリケーションを作成しようとしています。基本的なUIベースのビュー、コントローラー、モデルがありますが、これより高度で、RESTfulなjsonインターフェースを提供したいと思っていました。jsonをPlay 2で使用する

しかし、私は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); 
    } 
} 

.... 

しかし、私はこれを実行すると、私は次のエラーを取得:私の知る限り、私はdocumentationから

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(); 

コピーしましただから私はこれを働かせるための助けに感謝します。

答えて

0

documentationをチェックアウトしてみましたか? 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); 
    } 
} 
+0

アンソニー、私は再読み込みマニュアルとexmapleを持っており、それがより理にかなっています今私には、しかし、私は "シンボルを見つけることができません:クラスJson"というエラーが表示されます。私はplay.libs.jsonを含んでいます。ここに私のコードです:\t @ BodyParser.Of(Json。。。クラス) \tパブリック静的結果部材(ロングID){ \t JsonNode JSON =リクエスト()体()asJson(); \t ObjectNode結果= Json.newObject()。 。 \t文字列名= json.findPath( "名前")getTextValue(); \tメンバーメンバー= Member.byid(id); \t result.put( "id"、member.id); \t result.put( "電子メール"、member.email)。 \t result.put( "name"、member.name); \t return ok(結果); \t} – mstreffo

+0

私は上記の例に続いているが、私は次のエラーを取得するいくつかの理由:「シンボルを見つけることができません:クラスJSONを」 - 私は自分のアプリケーションのコントローラでplay.libs.Jsonを輸入しています。誰も助けることができますか?私は私のソースコードを改訂しましたし、今、新たなエラーを取得しています – mstreffo

+0

私はそれが – mstreffo

1

私の知る限り、使用しているコードは、これに応じて任意の公式プレイバージョン(2.0または2.0.1でもない)に達していない:https://github.com/playframework/Play20/pull/212

代わりに、あなたがこの(テストしていない)行うことができます。

if(request().getHeader(play.mvc.Http.HeaderNames.ACCEPT).equalsIgnoreCase("application/json")) { 
+0

おかげで(上記の更新質問内容を参照してください) – mstreffo

5

をプレイ2 documentatioでJsonクラスの使用で競合があるように見えますn。正しく動作して上記の例を取得するには、以下の輸入が使用されます。

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

の右Jsonクラスの明示的な呼び出しを?しかし、これが私の仕事の例を得ることができる唯一の方法です。

0

play.libs.JsonをインポートしてからとBodyParser.Of注釈を使用しました。

上記の注釈では、拡張するクラスがplay.mvc.BodyParserであると想定しています。したがって、@BodyParser.Of(Json.class)@BodyParser.Of(BodyParser.Json.class)に置き換えるだけです。

2

これを試してみてください:

import play.*; 
import play.mvc.*; 
import org.codehaus.jackson.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); 
    } 
    } 
+2

行くあなたのコード、参照リンク、explanantionのためにいくつかのより多くの情報を追加与える、 –

関連する問題