exceptionjersey-2.0exceptionmapper

Multiple ExceptionMappers in Jersey


Assume I have two ExceptionMappers:

public class WebApplicationExceptionMapper implements ExceptionMapper<WebApplicationException>
( class WebApplicationException extends RuntimeException )
public class GenericExceptionMapper implements ExceptionMapper<Throwable>

If WebApplicationException or sub exception of WebApplicationException thrown, which mapper will handle them? can someone please explain it?

Thanks


Solution

  • This is stated in the JAX-RS spec about ExceptionMappers

    When choosing an exception mapping provider to map an exception, an implementation MUST use the provider whose generic type is the nearest superclass of the exception.

    Basically, it's saying that your Throwable mapper will have the lowest priority, since it is the furthest supertype from any exception type. Any WebApplicationException or subclass of it will always use the WebApplicationException mapper.