Request body asJson return NullPointerException


(Sebastian) #1

Some body knows if I am using bad the BodyParser or the play api have a bug, but I am doing a project with Play Java and I have something like this

public Result create() {
JsonNode json = request().body().asJson();
User user = Json.fromJson(json, User.class);;
return ok(json);

so the debugger mode in the browser stand out the line JsonNode json = request().body().asJson() and says that I have a NullPointerException.

If somebody knows the solution I will acknowledge

(Greg Methvin) #2

asJson() returns null if the body is not JSON. The default body parser in Play detects the type based on the Content-Type header, so if you are sending the request make sure you’ve sent it with Content-Type: application/json. You can also use an explicit body parser to reject requests with an invalid content type using the Json parser, or allow them and force JSON parsing using the TolerantJson parser.

(Sebastian) #3

Yes, you are right.
I lacked the content-type header. Thank-you!