Using json with Play 2

I am trying to create a simple application that allows me to create, read, update and delete different users. I have a UI-based look, controller and model that work, but want to be more advanced than that and provide a RESTful json interface.

However, despite reading everything that I can find in the Play 2 documentation, in the Google Play 2 groups and on the stackoverflow website, I still can't get this to work.

I updated my controller based on previous reviews, and now I believe that it is based on 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 all we need!) String email = json.findPath("email").getTextValue(); if(name == null) { return badRequest("Missing parameter [email]"); } else { // Use the model 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 all we need!) 46 String email = json.findPath("email").getTextValue(); 

As far as I can tell, I copied from the documentation , so I would appreciate any help in its work.

+4
source share
6 answers

Apparently, there are conflicts in using the Json class in the Play 2 documentation. To correctly use the above example, 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); } } 

Note the explicit Json right class Json in @BodyParser

I'm not sure if this is a mistake or not? But this is the only way to make this example work.

+5
source

Import these two

 import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.ObjectNode; 

According to this documentation: http://fasterxml.imtqy.com/jackson-databind/javadoc/2.0.0/com/fasterxml/jackson/databind/node/ObjectNode.html

+3
source

Try the following:

 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
source

AFAIK, the code you use has not reached the official version of Play (not 2.0 or 2.0.1) according to this: https://github.com/playframework/Play20/pull/212

Instead, you can do this (not verified):

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

Have you tried checking the documentation ?

Serving a JSON response is as follows:

 @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
source

You imported play.libs.Json and then use the BodyParser.Of annotation with this Json.class .

In the above annotation, a class is expected that extends a play.mvc.BodyParser . So just replace @BodyParser.Of(Json.class) with @BodyParser.Of(BodyParser.Json.class) .

0
source

Source: https://habr.com/ru/post/1410976/


All Articles