Hi,
It is because the response content-type header is only set after the
response of the resource method has been processed.
For the case of exceptions thrown from the resource method then the
content-type will be set after the response of the exception mapper
has been returned.
I am gonna have to think of a way to improve the situation as a number
of developers are running into this issue. The problem is when one
sets a Response instance on the HttpResponseContext (returned or
generated from the resource method or an exception mapper) existing
response headers, if any, are removed i.e. it replaces the complete
response state.
If one explicitly sets response headers in a resource method one may
not necessarily want them to apply if an exception is thrown from that
resource method.
Another example is if an exception occurs in a response filter, which
is then mapped to a response, then it does not make sense to apply the
content-type (nor most likely any other response headers set by other
filters) associated with the resource method.
I need to think some more on this...
Paul.
On Sep 7, 2010, at 1:03 PM, exxos wrote:
> Hello,
>
> Here is the code I'm testing:
>
> @Path("hello")
> public class App {
>
> @Context HttpContext httpContext;
>
> @GET
> @Produces(MediaType.TEXT_HTML)
> public Response getHtml() {
>
> System.out.println("RESOURCE hello >>> HttpContext
> Response:" + httpContext.getResponse());
> System.out.println("RESOURCE hello >>> MediaType :" +
> httpContext.getResponse().getMediaType());
>
> return Response.ok("Hello I'm fine !").build();
> }
> }
>
> The issue is that MediaType is "null". Could you please advise why I
> cannot get the MediaType from the HttpContext?
> (This is the same behavior with ExecptionMapper where I base content
> negotiation on that)
>
> Best regards,
> exxos.