Skip to main content

Proxy API

Proxy based API lets reuse the existing JAX-RS annotated interfaces or implementation classes as proxies. The proxy call will lead to a proper URI and HTTP headers passed to the server, exactly the way the JAX-RS annotated service expects. Proxies can also check the actual HTTP Response, modify request URI and headers, as well as be converted to WebClients. For example, given the following sample interface and the client code:

@Path("books")
public interface BookStore {

   @GET
   @Path("{id}")
   @Produces("application/xml")
   Book getBook(@PathParam("id") long id);

   @POST
   @Consumes("application/xml")
   Response addBook(Book newBook);

   @Path("all")
   BookStore getStore();

   @GET
   Books getAll();

   public class RESTClient {

      public void consumeRESTfulService(String baseAddress) {
         BookStore proxy = 
            JAXRSClientFactory.create(baseAddress, BookStore.class);
        
         // get a single book
         Book book = proxy.getBook(1);
        
         // further introspect the response headers
         Client theClient = WebClient.getClient(proxy); 
         javax.ws.rs.core.Response response = theClient.getResponse();
         checkResponseHeaders(response.getMetadata()); 
        
         // add new book
         Response addNewBookResponse = proxy.addBook(new Book(
            "User Guide")); 
         if (201 == addNewBookResponse.getStatus()) {
            URI bookResourceLocation = 
               URI.create(addNewBookResponse.getMetadata().getFirst(
               "Location"));
            Book theBookProxy = JAXRSClientFactory.create(
               bookResourceLocation, Book.class);
            theBookProxy.getName();  
         } else {
            reportUnexpectedStatus(addNewBookResponse);
         }           

         // get all books
         BookStore proxy2 = proxy.getStore();
         Books books = proxy2.getAll();
      }

}

the proxy.getBook(1) call will lead to a 'GET http://localhost:8080/store/books/1' HTTP request being sent to the server, with Accept header set to application/xml.

Note the proxy.getStore() and proxy2.getAll() calls. The BookStore.getStore() resource method is a subresource locator as it has only a @Path annotation. This locator returns the BookStore root instance itself. Thus proxy.getStore() only returns a proxy and the subsequent proxy2.getAll() call will lead to the 'GET http://localhost:8080/store/books/all' HTTP request being sent.

Did this page help you?

If you find any issues with this page or its content – a typo, a missing step, or a technical error – let us know how we can improve!