This example demonstrates how to create a custom exception mapping. There are described several common use-cases such as WebApplicationException
, custom exceptions and an exception mapper inheritance.
The full description how to handle exception and map them to Response object can be found in Jersey User Guide, chapter WebApplicationException and Mapping Exceptions to Responses.
The mapping of the URI path space is presented in the following table:
URI path | HTTP methods | Allowed Values | Description |
---|---|---|---|
/exception | GET | --- | returns “ping!” |
/exception/webapplication_entity | POST | two numbers delimited by colons e.g. 1:201 | not handled by WebApplicationExceptionMapper (already has an entity in the exception) |
/exception/webapplication_noentity | POST | e.g. 1:201 | handled by WebApplicationExceptionMapper |
/exception/my | POST | e.g. 1:201 | handled by MyExceptionMapper |
/exception/mysub | POST | e.g. 1:201 | handled by MySubExceptionMapper |
/exception/mysubsub | POST | e.g. 1:201 | handled by MySubExceptionMapper |
/exception/request_exception | POST | String “Request Exception” | not reached a resource method, processed by ContainerRequestFilter |
/exception/response_exception | GET | --- | response handled and changed by ContainerResponseFilter |
Application is based on Grizzly container (see App
). Everything needed (resources/providers) is registered in ExceptionResource
and custom exceptions along with mappers can be found in Exceptions
class.
Run the example as follows:
mvn clean compile exec:java
This deploys the example using Grizzly container.