users@jersey.java.net

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

From: Jakub Podlesak <jakub.podlesak_at_oracle.com>
Date: Tue, 01 Nov 2011 14:15:53 +0100

Hi,

Right, you may utilize [1] to patch the request Accept header.

~Jakub

[1]http://jersey.java.net/nonav/apidocs/latest/jersey/com/sun/jersey/spi/container/ContainerRequestFilter.html

On 1.11.2011 11:11, qzhong wrote:
> Thanks for reply Santiago.
>
> If there is no way to sort the @produces methods in Jersey, I have
> to change the accept-header of the requests.
>
>
> On 28/10/11 15:46, Santiago Pericas-Geertsen wrote:
>> 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
>>
>