Pages

Wednesday, 14 May 2014

Jax-RS custom exception handling

One of the nice things about working with JEE is the components available really are pretty standard.  While using JAX-RS, sometimes you need to control how exceptions get processed and fed back to the user.  If an exception is thrown, by default, you'll get some horrible HTTP 500 internal server exception, exposing the internal failings of your web service.

Consider the following gist, this endpoint would be used to view a user based on the Id.

Now, the implementation of this interface would look something like the following, to do the donkey work of actually retrieving the user.

This looks fine, but consider if the userDao was doing some entity business logic, using Query.getSingleResult and the user with this ID didn't exist?

According to the JEE6 API documentation, you'd receive a NoResultException, which would cause an HTTP 500 error exposing your internal server exception, which is definitely something end users shouldn't see. We need to leverage the exception handling of Jax-RS!

First, we need a dumb exception object, appropriately named, which will be what we'll actually throw, consider the code below..

Next, we need to modify our original code to take into account this exception, I've modified the original UserWebService and associated implementation appropriately below.

This will now throw an appropriate exception when a user cannot be found. However, we still need to create a Handler object to convert this exception into an actual JSON response so we get a nice friendly error message. The class below handles this exception and will convert the error message in the exception into a JSON response. The important annotation you'll see on this class is the @Provider annotation.

You'll notice we create an ErrorMessage object to respond to from the web service. This is just a simple dumb object to hold the details of the actual error that'll be marshalled into JSON.

The final step in mapping our exception handler provider to the web-app is to add the following into our web.xml for our webapp.

Now when we call this REST endpoint with a user ID that doesn't exist (let's say "DAG"), we'll happily receive the following JSON response rather than a stacktrace.