users@jersey.java.net

Re: [Jersey] Shouldn't having a Produces annotation automatically set the Content-Type of the response?

From: António Mota <amsmota_at_gmail.com>
Date: Tue, 7 Apr 2009 13:47:57 +0100

When testing gets from the browser (FF, of course:) I found most
usefull the "Modify Headers" addon.

https://addons.mozilla.org/en-US/firefox/addon/967

The author is http://www.garethhunt.com/


_______________________________________________

Melhores cumprimentos / Beir beannacht / Best regards

António Manuel dos Santos Mota

_______________________________________________




2009/4/7 Paul Sandoz <Paul.Sandoz_at_sun.com>:
> Hi Rohan,
>
> You found a bug in the simple servlet sample, which i have fixed. The
> general GET method, "doGet", was not setting the media type.
>
> I changed the switch statement to:
>
>        switch (rep) {
>            case 0:
>                r = Response.ok(help, "text/plain").
>                        header("resource3-header", "text/plain").build();
>                break;
>            case 1:
>                r = Response.ok(getStringRep(arg1, arg2), "text/plain").
>                        header("resource3-header", "text/plain").build();
>                break;
>            case 2:
>                r = Response.ok(getFormURLEncodedRep(arg1, arg2),
> "application/x-www-form-urlencoded").
>                        header("resource3-header",
> "application/x-www-form-urlencoded").build();
>                break;
>            case 3:
>                r = Response.ok(getImageRep(), "image/jpg").
>                        header("resource3-header", "image/jpg").build();
>                break;
>            default :
>                r = Response.ok(help, "text/plain").build();
>                break;
>        }
>
> Essentially with this method it overrides what may be the msot acceptable in
> the accept header based on the quest parameter "rep".
>
>
> What you require is supported, it is just that the browser is going to the
> general GET method because the @Consumes is "*/*" by default in the absence
> of the annotation, and because the accept header of the browser has a higher
> preference for anything other than "text/plain",
> "application/x-www-form-urlencoded" or "image/jpg".
>
> If you do a curl, say:
>
> curl -v -H "Accept: image/jpg"
> http://localhost:8080/SimpleServlet/resources/resource3/fred/barney
>
> then this will go to the "getImageRep" method and the content type will be
> set to "image/jpg".
>
> Paul.
>
>
> On Apr 7, 2009, at 3:25 AM, Rohan Sahgal wrote:
>
>> Hi,
>> I have been using jersey1.0.1.
>>
>> I came across a situation where I wanted to return a dynamic image and
>> an audio file in response to a url.
>>
>> I followed the approach as shown in the Simple Servlet sample.
>>
>> However I noticed that if i goto the image url outlined in the sample,
>> the browser(firefox) tries to read it as an application/octstream type
>> of file. Since it does not know what to do, it gives the option to
>> save.
>>
>> I later changed the code to set the content type of the response by doing
>> r = Response.ok(getImageRep(), "text/plain").header("Content-Type",
>> "image/jpg").build();
>>
>> This works and the browser can successfully display the image.
>>
>> I was just wondering whether its possible for the response
>> content-type to be automatically filled by Jersey (depending on the
>> value of @Produces annotation)
>> If not, can I directly annotate my method somehow to put the
>> Content-Type header, I dont want to have wrapper methods like outlined
>> in this sample.
>>
>> Thanks,
>> Rohan
>>
>> ---------------------------------------------------------------------
>> 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
>
>