You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by icfantv <ad...@gmail.com> on 2013/08/22 18:00:09 UTC

Ensuring correct MediaType on ExceptionMapper

I'm modifying an existing ExceptionMapper we have that had previously,
explicitly set @Produces to use a media type of APPLICATION_XML only and set
this media type on the returned Response.

I'd like to add JSON to the @Produces and remove the code blurb that
explicitly set the media type on the returned Response but want to ensure
that CXF will use the "correct" media type depending on the original
request.  E.g., if the original request's Accepts header says JSON then the
mapper would return a JSON response:

@Override
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response toResponse(MyException myException) {

  // *snip*

  return Response.entity(...).build();  // no media type specified
}

Will this work as intended?



--
View this message in context: http://cxf.547215.n5.nabble.com/Ensuring-correct-MediaType-on-ExceptionMapper-tp5732905.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: Ensuring correct MediaType on ExceptionMapper

Posted by Sergey Beryozkin <sb...@gmail.com>.
On 23/08/13 00:00, icfantv wrote:
> Ok, we just wrote a unit test and removed the @Produces annotation and it
> works as we originally expected.  I.e., whatever the Accepts header value
> is, is the media type of the response.  We did not have to use HttpHeaders
> at all.
Sure, HttpHeaders can help if Accept has multiple values

Cheers, Sergey
>
>
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/Ensuring-correct-MediaType-on-ExceptionMapper-tp5732905p5732932.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>


-- 
Sergey Beryozkin

Talend Community Coders
http://coders.talend.com/

Blog: http://sberyozkin.blogspot.com

Re: Ensuring correct MediaType on ExceptionMapper

Posted by icfantv <ad...@gmail.com>.
Ok, we just wrote a unit test and removed the @Produces annotation and it
works as we originally expected.  I.e., whatever the Accepts header value
is, is the media type of the response.  We did not have to use HttpHeaders
at all.



--
View this message in context: http://cxf.547215.n5.nabble.com/Ensuring-correct-MediaType-on-ExceptionMapper-tp5732905p5732932.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: Ensuring correct MediaType on ExceptionMapper

Posted by icfantv <ad...@gmail.com>.
Oh wow.  That's not at all what I was expecting.  Thanks for the head's up
and clarification.



--
View this message in context: http://cxf.547215.n5.nabble.com/Ensuring-correct-MediaType-on-ExceptionMapper-tp5732905p5732928.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: Ensuring correct MediaType on ExceptionMapper

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi
On 22/08/13 19:00, icfantv wrote:
> I'm modifying an existing ExceptionMapper we have that had previously,
> explicitly set @Produces to use a media type of APPLICATION_XML only and set
> this media type on the returned Response.
>
> I'd like to add JSON to the @Produces and remove the code blurb that
> explicitly set the media type on the returned Response but want to ensure
> that CXF will use the "correct" media type depending on the original
> request.  E.g., if the original request's Accepts header says JSON then the
> mapper would return a JSON response:
>
> @Override
> @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
> public Response toResponse(MyException myException) {
>
>    // *snip*
>
>    return Response.entity(...).build();  // no media type specified
> }
>
> Will this work as intended?
>
Not really, there was a proposal to support Produces on Exception 
mappers in 2.0 but there was an agreement in the end that it probably 
would not work, given that the goal of the exception mappers is to 
produce an error response from a caught exception and thus the mappers 
should not worry about what a client Accepting as part of the non-error 
response.
Exception mappers can also be used to produce a non-error message, but 
JAX-RS itself does not provide any explicit support for such purposes, 
but indeed the mappers can work with 
HttpHeaders.getAcceptableMediaTypes, and set the media type to the first 
value in that list

Cheers, Sergey



>
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/Ensuring-correct-MediaType-on-ExceptionMapper-tp5732905.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>


-- 
Sergey Beryozkin

Talend Community Coders
http://coders.talend.com/

Blog: http://sberyozkin.blogspot.com