JAX-RS ExceptionMapper not catching IllegalArgumentException

Viewed 798

What can be reasons why the following ExceptionMapper fails to catch the IllegalArgumentException and return a BAD_REQUEST response to the client?

This ExceptionMapper is supposed to catch exceptions and respond with a 400, for errors like if the client sends a JSON Array instead of an expected JSON Object to a JAX-RS Resource. Additionally it provides a JSON Object to describe the error in greater detail.

@Provider
public class IllegalArgumentExceptionMapper implements ExceptionMapper<IllegalArgumentException> {
    public Response toResponse(IllegalArgumentException ex) {
        JsonObject json = Json.createObjectBuilder().add("message", "Body should be a JSON Object").build();
        return Response.status(Response.Status.BAD_REQUEST).entity(json).build();
   }
}

It is however not doing that, instead the client receives a 500 with a HTML formatted stacktrace in the response and the following in the logs:

[2017-07-03T03:47:02.635+0000] [Payara 4.1] [WARNING] [] [javax.enterprise.web] 
  [tid: _ThreadID=28 _ThreadName=http-listener-1(1)] [timeMillis: 1499053622635] 
  [levelValue: 900] [[ StandardWrapperValve[myApp.JaxRsConfiguration]:
  Servlet.service() for servlet myApp.JaxRsConfiguration threw exception
  java.lang.IllegalArgumentException: argument type mismatch

This is the resource operation:

@POST
public Response postSignup(JsonObject signupJson) throws UnprocessableEntityException { ... }

We are talking Java EE 7 here, I tried to run with Payara 4.1 and the stock JAX-RS Provider, no frameworks involved.

0 Answers
Related