users@jersey.java.net

[Jersey] How to avoid hard coding to produce both xml and json results?

From: <arun_at_flourishersolutions.com>
Date: Fri, 13 Apr 2012 07:14:37 -0700
Hi,

We have a situation while trying to implement REST services using jax-rs. The REST service api needs to be published. Service is expected to give the results in either json or xml as per the user's requirement.
How can I do without hard coding for json or xml?

Right now I have hard coded it for json like the following code snippet.

    @GET @Path("/{id}")
    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public Response getCustomer(@PathParam("id") String id) {
        Customer c = null;
        Response r = null;
        if(id != null){
            c = fetchCustomer(id);//fetchCustomer will return the customer with id
            Builder ob = Builder.create(c);
            r = Response.ok(ob.deliver(),MediaType.APPLICATION_JSON).build();
        }
        else{
            r = Response.status(400).build();
        }
        return r;
    }

How can I remove the hard coding and give the result in desired format?


Regards,
Arun P V
Assoicate Engineer
+91 80 25286146 +917795350850
#746, 1st Floor, Krishna Temple Road,
Adjacent to Karvy, 1st stage, Indiranagar,
Bangalore 560038