Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jersey ExceptionMapper and inheritance

Let's say I have 3 exception classes that I want to "catch":

  • Jersey's WebApplicationException
  • MyException extends RuntimeException
  • Exception

I want the WebApplicationException to be still mapped automagically by Jersey, MyException to be mapped by MyMapper and to be sure that any other exceptions are also mapped by MyFallbackMapper.

So because any exceptions (that I want to control) finally extend Exception, is there any guarantee that MyMapper (and not MyFallbackMapper) will be used for MyException?

like image 639
coffee_machine Avatar asked Oct 04 '12 07:10

coffee_machine


1 Answers

JAX-RS 1.1 spec, chapter 4.4:

When a resource class or provider method throws an exception, the JAX-RS runtime will attempt to map the exception to a suitable HTTP response - see section 3.3.4. An application can supply exception mapping providers to customize this mapping.

Exception mapping providers map a checked or runtime exception to an instance of Response. An excep- tion mapping provider implements the ExceptionMapper interface and is annotated with @Provider. When a resource method throws an exception for which there is an exception mapping provider, the match- ing provider is used to obtain a Response instance. The resulting Response is processed as if the method throwing the exception had instead returned the Response, see section 3.3.3.

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.

like image 156
Pavel Bucek Avatar answered Oct 05 '22 15:10

Pavel Bucek