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.

import javax.ws.rs.get;
import javax.ws.rs.produces;
import javax.ws.rs.core.mediatype;
import com.ibm.webapi.business.author;
import com.ibm.webapi.business.nonexistentauthor;

@registerprovider(exceptionmapperarticles.class)
public interface authorsservice {
  @get
  @produces(mediatype.application_json)
  public author getauthor(string name) throws nonexistentauthor; 
}

the getauthor method returns an object of the author class.

public class author {
   public string name;
   public string twitter;
   public string blog;
}

the actual invocation of the authors service happens in authorsservicedataaccess.java . the restclientbuilder is used to get an implementation of the authorsservice interface. the deserialization of the data into a java object is done automatically.

import org.eclipse.microprofile.rest.client.restclientbuilder;
import com.ibm.webapi.business.author;
import com.ibm.webapi.business.nonexistentauthor;

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).register(exceptionmapperauthors.class).build(authorsservice.class);
         return customrestclient.getauthor(name);
      } 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.

import org.eclipse.microprofile.rest.client.ext.responseexceptionmapper;
import com.ibm.webapi.business.nonexistentauthor;

@provider
public class exceptionmapperauthors implements responseexceptionmapper<nonexistentauthor> {
   @override
   public boolean handles(int status, multivaluedmap<string, object> headers) {
      return status == 204;
   }
   @override
   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.

 

 

 

 

Top