Hello,
First of all I would like to thank all the Jersey developers for the
JAX-RS effort. This API is really a pleasure to work with, especially
with Glassfish's EJB integration.
I liked it so much that I decided to use it not just for the REST API
but as a whole web application framework. With a small team of
developer we created a whole reservation portal in Poland based on
Jersey with it's JSP and EJB integration. It was a nice ride buy it
required some add-ons to get there. Now after the project is in
production I have some time to suggest those add-ons to the Jersey
community and probably re-implement them if accepted.
The first one in line is the exception mapping. The
WebApplicationException way seamed something really unacceptable to
us. We already had EJB services implemented and our own exception
hierarchy. Redefining that hierarchy to use WebApplicationException
would add a dependency to JAX-RS in our logic which just didn't seam
right. Also writing ale additional layer of EJB to pack our exceptions
to a WebApplicationException seamed as some unnecessary effort.
So we made the following decisions:
1. By default all exceptions should be outputted in the response so
that the calling application (AJAX client for example) can do the
appropriate user interaction.
2. Everything beyond that default should be defined by annotating the
exception class.
To accomplish this first we created a @WebThrowable annotation to set
the response code of and exception. This works like this:
@WebThrowable(statusCode=403)
public class AuthorizationException extends RuntimeException { ...
Then we created a class implementing ExceptionMapper<Throwable> which
build the Response in the following way:
return Response
.status(status) // taken from WebThrowable or
WebApplicationException or 500 by default
.type(mt) // depending on a configuration property plain
text, xml or json
.entity(e) // the exception
.build();
And finally we added a class implementing MessageBodyWriter<Throwable>
which depending on the configured media type prints the exception like
this:
- plain text - just prints the localizedMessage
- json - prints to JSON using JAXB
- xml - prints to XML using JAXB
I would like to ask the community would this type of functionality be
welcome in the official jersey release ?
Should it be implemented in the core library and activated by a
configuration property ?
Or should it be done in and library add-on which tries to expand
jersey to a whole web application framework ?
--
Adam Walczak
www.adamwalczak.info
+48 604 188 992