Hi
On 11/12/12 14:09, fplouffe wrote:
Using CXF 2.7.
Created REST service that can handle xml and json. The service is working
correctly, that is depending on accept and content-type headers, the service
read/write xml or json.
I want to create a custom error message. I registered custom ExceptionMapper
class in the<jaxrs:provider>. This class extends
ExceptionMapper<WebApplicationException>.
When I submit a request with a bad message, my custom exception mapper kicks
in and I receive the custom error message in either xml or json, depending
on the accept header.
However, if I send a request to a non-existing service, or submit a POST to
a service that only handle GET, my custom exception mapper still kicks in
but I receive the 'No message body writer has been found for response class
MyError' error message.
I did a bit of tracing. In JAXRSOutInterceptor, method serializeMessage, the
call to computeAvailableContentTypes returns an empty array in the case I
get the error. Since the array is empty, it skip the loop where it is
supposed to set the writers object.
I can hard code a media type to the response object in my custom exception
mapper to either application/xml or application/json and it work. But that
is defeating the purpose of accept header where it should create the message
in the requested format.
How do I get my custom error message to be marshalled in the requested
format for all WebApplicationException?
You can get HttpHeaders injected and get the first value from the list
of acceptable types it returns and set it as Response type.
I guess the runtime should default itself to the first acceptable type
but what if the request is made from the browser, will MyError be
properly represented as text/html for example, I'm not sure.
I think if the runtime has caught the exception which was thrown from
the application method then the intersection of media types will work,
but if we have a case where the exception has been thrown before the
method has been found and thus no @Consumes values are visible, then
unless the exception mapper sets something itself, the runtime can not
figure out the correct type itself...
Do you agree ? I might seek some clarifications on my own, but I guess
this is how it is supposed to work...
Cheers, Sergey
Thanks
Francois
--
View this message in context:
http://cxf.547215.n5.nabble.com/Custom-ExceptionMapper-No-message-body-writer-has-been-found-for-response-class-MyError-tp5720034.html
Sent from the cxf-user mailing list archive at Nabble.com.