users@jersey.java.net

Re: [Jersey] Representation Type Determined by Query Parameter?

From: Charles Overbeck <coverbec_at_pacbell.net>
Date: Tue, 9 Mar 2010 21:55:41 -0800 (PST)

Hi Paul,



Thanks, that helps a lot.

Cheers,

Charles



----- Original Message ----
From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
To: users_at_jersey.dev.java.net
Sent: Tue, March 9, 2010 12:44:09 AM
Subject: Re: [Jersey] Representation Type Determined by Query Parameter?

Hi Charles,

Jersey supports URI-based conneg using a suffix at the end of the path.

You can declare in your web.xml a mapping of suffix to media type:
  https://jersey.dev.java.net/nonav/apidocs/latest/jersey/com/sun/jersey/api/core/ResourceConfig.html#PROPERTY_MEDIA_TYPE_MAPPINGS

if you want to do the same using a query parameter you could write a Jersey request filter that does similar things to the suffix filter:

http://fisheye4.atlassian.com/browse/jersey/trunk/jersey/jersey-server/src/main/java/com/sun/jersey/api/container/filter/UriConnegFilter.java?r=HEAD

which modifies the accept header based on the suffix value.

Hth,
Paul.

On Mar 9, 2010, at 5:30 AM, Charles Overbeck wrote:

> Hello,
>
> I have code like this:
>
> ...
> @GET
> @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_JSON})
> public JAXBElement<CurrenciesType> listSupportedCurrencies() {
> CurrencyService currencyService = (CurrencyService) getService();
> CurrenciesType currenciesType = getSupportedCurrencies();
> return new ObjectFactory().createCurrencies(currenciesType);
> }
>
> I would like to control whether the response is JSON or XML via a query parameter, e.g., /currencies?fmt=json, or /currencies?fmt=xml.
>
> With the above, I know I can control whether the response is JSON or XML via the HTTP Accept request header. I also know that I can programatically build up a Reponse and return that.
>
> But a client would prefer a query parameter controlling the format, as he feels it is more visible and obvious. And I like my method signature the way it is now, instead of returning a Response, as it is more self-documenting.
>
> Any thoughts or ideas are appreciated.
>
> Thanks,
>
> Charles
>
>
> ---------------------------------------------------------------------
> 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