users@jersey.java.net

Re: [Jersey] Setting Request Content Type Dynamically

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Wed, 16 Dec 2009 20:04:23 +0100

On Dec 15, 2009, at 11:34 PM, cgswtsu78 wrote:

>
> Hello,
>
> Does anyone know how I can use a querystring parameter to set the
> content
> type of the request.
>
> For example if I have a resource method that accepts both XML and
> JSON, how
> can I let the consumer (via a url GET request) decide what content
> type they
> want to be returned?


Usually the client will send an Accept header declaring the media
types it accepts and the server will select the most preferable media
type to be used as the content-type of the response.

Jersey does have a feature to support what is referred to as URI
content negotiation, such that suffixes in the URI path can be mapped
to an acceptable media type e.g.

  GET /topspamsenderchart.json

would return JSON.

See:

https://jersey.dev.java.net/nonav/apidocs/latest/jersey/com/sun/jersey/api/core/ResourceConfig.html
#getMediaTypeMappings%28%29

which can be set in by a class that extends from ResourceConfig and is
registered to declare the root resource and provider classes.

In 1.1.5-ea-SNAPSHOT Pavel implemented a way to declare the suffix to
media type mappings in a web.xml, see:

https://jersey.dev.java.net/nonav/apidocs/1.1.5-ea-SNAPSHOT/jersey/com/sun/jersey/api/core/ResourceConfig.html
#PROPERTY_MEDIA_TYPE_MAPPINGS

Paul.


> Is there an annotation I can use in order to map a
> querystring request parameter to dynamically set the requests
> content type?
>
>
> @GET
> @Path("/topspamsenderchart")
> @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
> public Object getTest()
> {
>
> Object o = something.fetch;
>
> return o;
> }
> --
> View this message in context: http://n2.nabble.com/Setting-Request-Content-Type-Dynamically-tp4172634p4172634.html
> Sent from the Jersey mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>