Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing json with Play 2
    text
    copied!<p>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.</p> <p>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. </p> <p>I've updated my controller based on previous feedback and I now believe it is based on the documentation.</p> <p>Here is my updated controller:</p> <pre><code>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); } } .... </code></pre> <p>But when I run this, I get the following error:</p> <pre><code>incompatible types [found: java.lang.Class&lt;play.libs.Json&gt;] [required: java.lang.Class&lt;?extends play.mvc.BodyParser&gt;] 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(); </code></pre> <p>As far as I can tell, I've copied from the <a href="http://www.playframework.org/documentation/2.0.1/JavaJsonRequests" rel="nofollow">documentation</a> so I would appreciate any help in getting this working.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload