Advertisements

Continued from:

MicroProfile Rest Client

This module lets us implement a simple client for a Restful service that we need to consume. We just need to create an interface with JAX-RS annotations describing the behavior of the Restful service.

Advertisements
@Path("/stock")
public interface StockClient {
   @GET
   @Produces(MediaType.APPLICATION_JSON)   	 
   List getAll();
    
   @GET
   @Produces(MediaType.APPLICATION_JSON)
   @Path("/status/{status}")   	 
   List getByStatus(@PathParam("status")String status);
    
   @GET
   @Path("/{id}")
   @Produces(MediaType.APPLICATION_JSON)
   Stock findById(@PathParam("id") String id);
}

And then, we can use the RestClientBuilder and our client interface to generate a proxy instance to call the methods in a nicer way. 

public Stock getStock(String id){
   URI apiUri = getURI();
   StockClient stockClient = RestClientBuilder.newBuilder()
       .baseUri(apiUri)
       .build(StockClient.class);
   return stockClient.findById(id);
}
Table of contents:

Advertisements

Pages: 1 2 3 4 5 6 7 8

Leave a Reply

Your email address will not be published. Required fields are marked *