Invoking REST APIs From Java Microservices

Freviously, I blogged about how to implement and document REST APIs in JavaEE applications with Eclipse MicroProfile. In this article, I describe the inverse scenario — how services can invoke other services via REST APIs over HTTP.

MicroProfile comes with a REST Client which defines a type-safe client programming model. The REST Client makes it easier to convert between the JSON data and Java objects in both directions.

There is pretty good documentation about the REST Client available (see below). In this article, I describe how I’ve used the client in my sample application. The application has a Web API service which implements the BFF (backend for front-end pattern). The Web API service uses the REST Client to invoke another ‘Authors’ service.

Microservice clients

Get the code of the cloud-native starter application.

First, you need to define the interface of the service you want to invoke.


public interface AuthorsService {
  public Author getAuthor(String name) throws NonexistentAuthor; 

The getAuthor method returns an object of the Authorclass.

public class Author {
   public String name;
   public String twitter;
   public String blog;

The actual invocation of the authors service happens in The RestClientBuilder is used to get an implementation of theAuthorsService interface. The deserialization of the data into a Java object is done automatically.


public class AuthorsServiceDataAccess {
   static final String BASE_URL = "http://authors/api/v1/";
   public AuthorsServiceDataAccess() {} 
   public Author getAuthor(String name) throws NoConnectivity, NonexistentAuthor {
      try {
         URL apiUrl = new URL(BASE_URL + "getauthor?name=" + name);
         AuthorsService customRestClient = RestClientBuilder.newBuilder().baseUrl(apiUrl).
      } catch (NonexistentAuthor e) {
         throw new NonexistentAuthor(e); 
      } catch (Exception e) {
         throw new NoConnectivity(e);

In order to use the RESTClientBuilder, you need to understand the concept of the ResponseExceptionMapper. This mapper is used to translate certain HTTP response error codes back into Java exceptions.

public class ExceptionMapperAuthors implements ResponseExceptionMapper<NonexistentAuthor> 
   public boolean handles(int status, MultivaluedMap<String, Object> headers) {
      return status == 204;
   public NonexistentAuthor toThrowable(Response response) {
      switch (response.getStatus()) {
         case 204:
            return new NonexistentAuthor();
        return null;

Read the following resources to learn more about the MicroProfile REST Client.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s