users@jersey.java.net

[Jersey] How to customize ExceptionMapper's response format

From: William Zhou <wlmzhou_at_yahoo.com>
Date: Mon, 15 Jun 2009 20:28:04 -0700 (PDT)

Hi,

I am writing a custom exception mapper, saying BadRequestExceptionMapper<BadRequestException> implements ExceptionMapper<BadRequestException>.
I am planning to provide more information to the client than just returning the error status 400 in the mapper, by filling an entity in the response.

The problem now is that the customer may require to accept xml or json type in his request. In the error response of BadRequestExceptionMapper, I would like to convert the entity to the correct format. However, I did not find a way because the ExceptionMapper interface only passes the exception into the mapper: javax.ws.rs.core.Response toResponse(E e) and the format info is lost.

I am wondering is there ways to customize the media type of my error response?
1) Is it possible to inject the uriInfo or request, which contains the acceptable media type infomation, into the exception mapper?
2) Is it possible to rewriting the response of the BadRequestExceptionMapper in another provider, which is aiming to do reformating?
3) Put the media type info in BadRequestException is one way, but not my prefered.

Thanks a lot,
William