Igor Kromin |   Consultant. Coder. Blogger. Tinkerer. Gamer.

Previously I wrote a post about using a custom exception mapper in your RESTful service with Jersey. This article looks at the execution order of all of the filters, interceptors, readers and writers that get called along the way in this scenario.

This article relies on the code and some of the explanation from the following articles, be sure to go over those first:

I am not covering the execution order prior to getting to the Resource class as that's been covered in the previous articles I've written (linked above) and since it varies depending on whether you use GET or POST methods and if any message body readers are used.

So lets see what happens when a request is made to the /exception URI of the service that's been built up so far...
jerseryflow_exception.png


Since the Resource class in this case simply throws a HelloException, execution shifts straight to the ExceptionMapper. Then the container response filter and writer interceptor are called. Behaviour after this point depends on how the exception mapper is implemented.



I've implemented two versions of the exception mapper, one that used a standard String entity and one that used the actual HelloException as the entity itself. The latter version meant that an entity writer (message body writer) is used to transform the exception (entity) into output from the service. If the Exception is used as the entity, the message body writer is invoked, otherwise nothing further from your service is called.

The execution order is quite straight forward for cases where an exception is thrown from your Resource code, and can be very powerful. This approach can be used to catch and process exceptions and to generate valid responses from your service, instead of simply letting Jersey return the usual 500 error.

-i

A quick disclaimer...

Although I put in a great effort into researching all the topics I cover, mistakes can happen. Use of any information from my blog posts should be at own risk and I do not hold any liability towards any information misuse or damages caused by following any of my posts.

All content and opinions expressed on this Blog are my own and do not represent the opinions of my employer (Oracle). Use of any information contained in this blog post/article is subject to this disclaimer.
Hi! You can search my blog here ⤵
NOTE: (2022) This Blog is no longer maintained and I will not be answering any emails or comments.

I am now focusing on Atari Gamer.