users@jersey.java.net

Re: [Jersey] User controlled response

From: Marc Hadley <Marc.Hadley_at_Sun.COM>
Date: Mon, 09 Nov 2009 18:02:41 -0500

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
>