users@jersey.java.net

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

From: Tatu Saloranta <tsaloranta_at_gmail.com>
Date: Fri, 13 Apr 2012 09:49:38 -0700

On Fri, Apr 13, 2012 at 7:14 AM, <arun_at_flourishersolutions.com> wrote:
> 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?

Just remove it? Why are you adding content type manually in the first
place? That's what JAX-RS impl is there to do...

-+ Tatu +-