playframework Working with JSON - Scala Java: Accepting JSON requests with BodyParser

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

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



Got any playframework Question?