Red Hat JBoss Fuse - Developing RESTful Web Services - Mapping exceptions to responses
here are instances where throwing a WebApplicationException
exception is impractical or impossible. For example, you may not want to catch all possible exceptions and then create a WebApplicationException
for them. You may also want to use custom exceptions that make working with your application code easier.
To handle these cases the JAX-RS API allows you to implement a custom exception provider that generates a Response
object to send to a client. Custom exception providers are created by implementing the ExceptionMapper<E>
interface. When registered with the Apache CXF runtime, the custom provider will be used whenever an exception of type E is thrown.
Exception mappers are used in two cases:
When a
WebApplicationException
, or one of its subclasses, with an empty entity body is thrown, the runtime will check to see if there is an exception mapper that handlesWebApplicationException
exceptions. If there is the exception mapper is used to create the response sent to the consumer.When any exception other than a
WebApplicationException
exception, or one of its subclasses, is thrown, the runtime will check for an appropriate exception mapper. An exception mapper is selected if it handles the specific exception thrown. If there is not an exception mapper for the specific exception that was thrown, the exception mapper for the nearest superclass of the exception is selected.
If an exception mapper is not found for an exception, the exception is wrapped in an ServletException
exception and passed onto the container runtime. The container runtime will then determine how to handle the exception.
Read full article from Red Hat JBoss Fuse - Developing RESTful Web Services - Mapping exceptions to responses
No comments:
Post a Comment