users@jersey.java.net

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

From: Triggs, Julian (Sigma) <"Triggs,>
Date: Mon, 16 Apr 2012 07:44:00 +0100

Hi Tatu,
See below


--------------------------------------------------------------------------
AstraZeneca UK Limited is a company incorporated in England and Wales with registered number: 03674842 and a registered office at 2 Kingdom Street, London, W2 6BD.
Confidentiality Notice: This message is private and may contain confidential, proprietary and legally privileged information. If you have received this message in error, please notify us and remove it from your system and note that you must not copy, distribute or take any action in reliance on it. Any unauthorised use or disclosure of the contents of this message is not permitted and may be unlawful.
Disclaimer: Email messages may be subject to delays, interception, non-delivery and unauthorised alterations. Therefore, information expressed in this message is not given or endorsed by AstraZeneca UK Limited unless otherwise notified by an authorised representative independent of this message. No contractual relationship is created by this message by any person unless specifically indicated by agreement in writing other than email.
Monitoring: AstraZeneca UK Limited may monitor email traffic data and content for the purposes of the prevention and detection of crime, ensuring the security of our computer systems and checking Compliance with our Code of Conduct and Policies.
-----Original Message-----
From: Tatu Saloranta [mailto:tsaloranta_at_gmail.com]
Sent: 13 April 2012 17:50
To: users_at_jersey.java.net
Subject: [Jersey] Re: How to avoid hard coding to produce both xml and json results?

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.
[Julian Triggs]
[Julian Triggs]
[Julian Triggs] The server can return either format according to what you've coded in @Produces
Jersey will return either of these according to what the client demands when the client specifies .accept([mediaType]) when the client is building up the request
BUT see my posting of about a week ago entitled 'JSON anomaly'

> 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 +-