Java – play! The framework returns a JSON response
•
Java
I use play! Framework 2.0 and I are new in this framework How to return only the JSON representation of my model in a white HTML page?
What am I doing
public static void messagesJSON(){
List<Message> messages = Message.all();
renderJSON(messages);
}
But I received an error: you cannot use a method that returns unit as a handler
Solution
The method you use is from play 1 x. It is slightly different in play 2.0 From the documentation, the following is an example of how to reply to a sayhello JSON request
@BodyParser.Of(Json.class)
public static Result sayHello() {
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);
}
}
Return an important part of the return OK (result) of a JSON objectnode
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
