users@jersey.java.net

Re: [Jersey] Jersey for clients

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Fri, 10 Oct 2008 18:10:29 +0200

On Oct 10, 2008, at 5:40 PM, Paulo Cordeiro wrote:

> Hi,
>
> Thank you. I need access service
>
> @GET
> @Path("/list")
> @ProduceMime( { "application/json", "application/xml" })
> public DpReports getListOfReports() {....
>
> and others similar and secure services.
>

Secured in what way? https?

   Client c = Client.create();
   WebResource r = c.resource("https://host/list");

   // JAXB using XML serialization
   DpReports dp = r.accept("application/xml").get(DpReports.class);

   // JAXB using JSON serialization
   DpReports dp = r.accept("application/json").get(DpReports.class);

   // JSON as String serialization
   String json = r.accept("application/json").get(String.class);


It you get fed up setting the accept header you can write a client
filter to add it and add the filter to the client or the Web resource.

   // I prefer JSON over XML filter
   ClientFilter cf = new ClientFilter() {
     public ClientResponse handle(ClientRequest cr)
             throws ClientHandlerException {
        cr.getMetadata().add("Accept", "application/json, application/
xml;q=0.8");
        return getNext().handle(cr);
     }
   }
   c.addFilter(cf);

Paul.