users@jersey.java.net

Re: [Jersey] Getting Accept Header in Requests

From: Marc Hadley <Marc.Hadley_at_Sun.COM>
Date: Fri, 04 Dec 2009 12:23:37 -0500

On Dec 4, 2009, at 11:02 AM, Robert Naczinski wrote:
>
> @GET
> @Produces(MediaType.TEXT_PLAIN + ", " + MediaType.TEXT_HTML)
> public Response read(@HeaderParam("Accept") String accept) {
> System.out.println("I was here");
> if (accept == MediaType.TEXT_PLAIN) {
> return Response.status(Status.OK).entity("text").type(MediaType.TEXT_PLAIN).build();
> } else {
> return Response.status(Status.OK).entity("<html><body>text</body></html>").type(MediaType.TEXT_HTML).build();
> }
> }
>
> My question was: is this accept implicit?
>
The above won't work, Accept isn't a single valued header and could look something like: Accept: text/html; q=1.0, text/plain; q=0.8, */*; q=0.1.

You do much better either splitting up the methods as suggested by Frank or using Request.selectVariant[1] to have the framework do the content negotiation for you.

Marc.

[1] https://jsr311.dev.java.net/nonav/releases/1.1/javax/ws/rs/core/Request.html#selectVariant(java.util.List)

> Thanx
> Robert
>
> 2009/12/4 Paul Sandoz <Paul.Sandoz_at_sun.com>:
>>
>> On Dec 4, 2009, at 4:18 PM, Robert Naczinski wrote:
>>
>>> Hello,
>>>
>>> I can annotate resource method with '_at_Produces'-Annotation
>>> (https://jsr311.dev.java.net/nonav/releases/1.1/javax/ws/rs/Produces.html)
>>> In this annotation you can have one or several values.
>>>
>>> Can I automatically get this value of HTTP Header?
>>>
>>> With @HeaderParam can I get this value. But can I get this implicit?
>>>
>>
>> I do not understand what you mean. Can you provide a concrete example of
>> what to want to do?
>>
>> Paul.
>>
>>> Is there 'Best Practice'?
>>>
>>> Thanks
>>> Robert
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>