users@jersey.java.net

Multiple resource formats

From: Farrukh Najmi <farrukh_at_wellfleetsoftware.com>
Date: Fri, 30 Jan 2009 15:32:28 -0500

I suspect this one has been discussed before but I did not find anything
in archives...

In me jersey server I want to expose the same resource in multiple
output formats. The current thinking is that the format will be
specified in the URL as a parameter rather than a path segment of the URL.

Thus to get XML response the URL pattern would be:

/search?queryId=foo&queryParams1=bar&format=text/xml

and to get JSON response the URL pattern would be:
/search?queryId=foo&queryParams1=bar&format=application/json

This would require having two methods in the server with same @Path but
different @Produces annotations:

    @Path("/search")
    @GET
    @Produces("text/xml")
    public Response searchXML() throws JAXBException {
        String resp = searchInternal();
        return Response.status(200).entity(resp).build();//"OK"
    }

    @Path("/search")
    @GET
    @Produces("application/json")
    public Response searchJSON() throws JAXBException {
        String resp = searchInternal();
        return Response.status(200).entity(resp).build();//"OK"
    }


What I am not sure of is how to specify the "format" parameter for each
method.

Thanks for your help.

-- 
Regards,
Farrukh
Web: http://www.wellfleetsoftware.com