When I used json in my controller class of Play! framework, I experienced something that I didn't know why.
I had two ajax calls so my controller class had two corresponding method like below:
@BodyParser.Of(BodyParser.Json.class)
public static Result categoryAdder() {
....(using json)....
}
Then,
@BodyParser.Of(BodyParser.Json.class)
public static Result pathCalculator() {
MultipartFormData body = request().body().asMultipartFormData();
FilePart filePart = body.getFile("imageFile");
ObjectNode jsonResult = Json.newObject();
....
}
I used ajaxForm that is a plug-in for jquery to pass a file as multipartFormData.
When I put @BodyParser.Of(BodyParser.Json.class) before method pathCalculator(), this method threw an null pointer exception on FilePart filePart = body.getFile("imageFile");. This meant the request didn't have any file inside. However, when I removed @BodyParser.Of(BodyParser.Json.class) from the method pathCalculator(), it worked well. Does @BodyParser.Of(BodyParser.Json.class) be needed? I don't know why. The weird thing is that the method categoryAdder() works well during @BodyParser.Of(BodyParser.Json.class) is placed before.
Is there someone who knows why this situation happens?