First lets define a custom exception we'll be throwing. It's not strictly required to have a custom exception but for the purposes of this article it makes it easier to see what's going on if we do. The class itself is not remarkable and is as simple as it can get really...
Next we need a resource class that throws this exception. The class implements a simple GET method on the /exception URI and throws a new HelloException with the message "Hello!" Again nothing remarkable - just demo code.
If the service is invoked at this point the output will be "Request failed." like shown in the screenshot below...
Jersey will also emit a ServletException with the cause set to HelloException to the WebLogic standard output. This isn't exactly graceful nor the result we want, so read on.
To stop the ServletException being thrown we can add a very simple ExceptionMapper implementation for the HelloException class. With this in place, Jersey will use the Response from the implementation of this class instead of its default. This implementation uses a String entity with "ERROR: " appended to the exception message. The HTTP response status is set to 500.
Making the same GET request again results in different behaviour now. We get this message back - "ERROR: Hello!" Nothing is logged by Jersey or WebLogic either - since the exception mapper implementation doesn't do any logging.
That first implementation is a step forward but we can do better. Instead of relying on Jersey to generate output from the String entity we can use a message body writer to convert HelloException to data on the Response output stream.
First the return statement in the exception mapper must be modified to this:
This sets the entity to the HelloException instance itself. Then we add this body writer that will match write requests for HelloException...
The body writer implements three methods - isWriteable(), getSize() and writeTo(). The isWriteable() method simply makes sure this writer is being used to write a HelloException class. The getSize() method is not used. The writeTo() method is where all the real action happens.
In the writeTo() method we create a BufferedWriter and a PrintStream and then write a String to the buffered writer, then flush it. Afterwards the stack trace for the exception is written to the print stream. Nothing complicated here, just outputting the exception to the output stream. This ends up as output from your service and looks like this...
The body writer is quite simple in this case but serves to show how exceptions can be handled and written to the output of a service. In more complex scenarios this approach can be used to convert exceptions to JSON objects or even to generate a custom response message from your service.
Don't forget to register() the above classes in your ResourceConfig implementation or add the @Provider annotation if you're using automatic feature discovery to make all this work.
If you want to know where the exception mapper fits into the execution order in Jersey, see this article - Jersey JAX-RS filters and interceptors execution order when throwing Exceptions.