users@jersey.java.net

[Jersey] Re: How can I change/retain the order of _at_produces methods?

From: Santiago Pericas-Geertsen <Santiago.PericasGeertsen_at_oracle.com>
Date: Fri, 28 Oct 2011 10:46:47 -0400

On Oct 27, 2011, at 1:10 PM, qiaozhong_at_gmail.com wrote:

> @POST
> @Produces("application/json")
> public String postStuff()
>
> @POST
> @Produces("text/html")
> public String postStuffHtml()
>
> Use IE to access the url.
>
> 1. If accept header is default, return html.
> IE's default Accept header: 'image/jpeg,
> application/x-ms-application, image/gif,application/x-shockwave-flash,
> */*'
> Sometimes it's just '*/*'
>
> 2. Return json if accept header is 'application/json, */*'

 Well the */* without explicit listing of text/html just throws the content negotiation algorithm out of the window. I don't see a way to fix this unless you inspect the Accept header by hand.

> The problem is that I can't find a good way to change/retain the order
> of the 2 @produces.
>
> - So if the method with @Produces("application/json") comes first, in
> case 1 json will always match '*/*' first and returned.

 You do not want to rely on any implementation depend ordering.

> - If I add 'qs=5' for @Produces("text/html"), then case 1 is fixed. But
> in case 2 html will be returned .

 I think this is because both text/html;qs=5 and application/json are applicable (due to */*) and the former is sorted before the latter (due to qs).

> IE's header is urgly, but I wonder if there is anyway in jersey to
> solve this issue. Just need to make sure the @Produces("text/html") is
> in the first place then problem solved.

 IE needs to do something about this incredibly vague Accept header. They cannot possibly accept text/html and foo/bar with equal weight. You could also inject a ContainerFilter that looks at the User-Agent and patches the Accept header.

-- Santiago