users@jersey.java.net

Re: [Jersey] User controlled response

From: Louis Polycarpou <louis_at_adjoovo.com>
Date: Mon, 9 Nov 2009 23:11:53 +0000

I have a related requirement to David's. If I have a list of mime
types that my resource can consume, how do I find out from within my
resource method which mime-type Jersey 'accepted' in order to fire the
current method?

@GET
@Consumes("text/plain,application/json")
@Path("{id}")
     public String getId(...) {
         // if I'm consuming JSON do this...

        // else if it's plain text do this...
     }

Thanks,
Louis

On 9 Nov 2009, at 23:02, Marc Hadley wrote:

> On Nov 9, 2009, at 4:37 PM, dloy wrote:
>
>> I want to create the response format (i.e. JSON, XML, ...) in
>> Jersey. Does Jersey provide a mechanism for a program to capture
>> the accepted Mimetype, as determined by Jersey, without Jersey
>> creating the response format.
>>
> If I understand the question correctly. You can use
> Request.selectVariant[1] to have Jersey pick the best matching
> alternative from a set of available formats that you specify using a
> list of Variants. E.g. if you supported JSON and XML for a
> particular resource you can write something like:
>
> @Context Request request;
>
> List<Variant> variants = Variant.mediaTypes
> (MediaType.APPLICATION_XML_TYPE, MediaType.APPLICATION_JSON_TYPE).add
> ().build();
> Variant selected = request.selectVariant(variants);
> MediaType selectedType = selected.getMediaType();
>
> The above will look at the HTTP Accept header in the request and
> determine the best match out of the list you provide.
>
> HTH,
> Marc
>
> [1] https://jsr311.dev.java.net/nonav/releases/1.0/javax/ws/rs/core/Request.html#selectVariant(java.util.List)
>
>> Thanks
>> David
>>
>>
>> ---------------------------------------------------------------------
>> 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
>


Louis Polycarpou (founder)


Adjoovo
SERVICE-ORIENTED COLLABORATION
http://adjoovo.com
http://twitter.com/adjoovo
+44 (0) 7786 136653

Louis Polycarpou
louis_at_adjoovo.com