@BodyParser.Of(BodyParser.Json.class)
public Result sayHello() {
JsonNode json = request().body().asJson();
String name = json.findPath("name").textValue();
if(name == null) {
return badRequest("Missing parameter [name]");
} else {
return ok("Hello " + name);
}
}
Hint: The advantage of this way is that Play will automatically respond with an HTTP status code 400 if the request was not a valid one (Content-type was set to application/json
but no JSON was provided)