vendredi 8 mai 2015

How to get access to all information, passed within the response from a RestEasy service to a RestEasy client?

i´m implementing a Restful service using Jax-RS 2.0 (Resteasy 3.0.7.Final) and share the interface between client and service.

The interface i´m sharing has several create Methods. The return value is void because ClientResponse is deprecated since RestEasy introduced JAX-RS 2.0 in version 3+.

To return the location of the new created object i inject the response, using the @Context annotation, and add the Content-Location header.

A simple example:

Shared Interface:

   @Path("/")
   @Consumes("application/xml")
   @Produces("application/xml")
   interface Resource {

        @Path("createSomething")
        void createSomething(AnyObject object);

        ...
   }

Implementation:

    class ResourceImpl {

         ...
         @Context org.jboss.resteasy.spi.HttpResponse response;
         ...

         @Override
         void createSomething(AnyObject object) throws AnyException {

             String id = service.create(object);

             response.getOutputHeaders().putSingle("Content-Location",
                  "/createSomething/" + id);

             response.setStatus(Response.Status.CREATED.getStatusCode());
         }

    }

The client (build with the Resteasy Proxy Framework):

     ...
     ResteasyClient client = new ResteasyClientBuilder().build();
     ResteasyWebTarget target = client.target(baseUrl);

     Resource resource = (Resource) target.proxy(Resource.class);   

     resource.createSomething(anyObject);
     ...

How can i retrieve Header information (and others, like Atom Links) which injected by the service?

Is it reasonable to use client side Filters and Interceptors?

Thank You

Aucun commentaire:

Enregistrer un commentaire