Exceptions are as important to handle in RESTful services as in any normal programming code. Errors in RESTful services should be mapped to the appropriate response code, as shown in the image retrieval example. JAX-RS provides a generic way of mapping Java exceptions to custom responses to the way you design. There are two approaches to mapping Java exceptions to HTTP responses:
- Declaring custom web application exceptions
- Mapping existing exceptions to error responses
In the following sections, we are going to examine each of the two approaches.
Web application exceptions are a custom type of exception, customized for JAX-RS to include a fully-detailed response with this exception. To declare your own, just create a class extending the WebApplicationException
class and pass to the super
's constructor an instance of the response class with your desired content that describes the error details, as discussed in the Custom responses...