users@jersey.java.net

Representation Type Determined by Query Parameter?

From: Charles Overbeck <coverbec_at_pacbell.net>
Date: Mon, 8 Mar 2010 20:30:21 -0800 (PST)

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