DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
What's in store for DevOps in 2023? Hear from the experts in our "DZone 2023 Preview: DevOps Edition" on Fri, Jan 27!
Save your seat
  1. DZone
  2. Data Engineering
  3. Data
  4. Invoking REST APIs From Java Microservices

Invoking REST APIs From Java Microservices

In this quick post, we take a look at how services can invoke other services via REST APIs over HTTP.

Niklas Heidloff user avatar by
Niklas Heidloff
CORE ·
Mar. 15, 19 · Tutorial
Like (7)
Save
Tweet
Share
37.33K Views

Join the DZone community and get the full member experience.

Join For Free

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.

  • guide: consuming restful services with template interfaces
  • rest client for microprofile
  • microprofile rest client in istio
  • java microservices with microprofile – rest client and json-b
REST Web Protocols Java (programming language) microservice

Published at DZone with permission of Niklas Heidloff, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Architecture and Code Design, Pt. 2: Polyglot Persistence Insights To Use Today and in the Upcoming Years
  • Implementing Infinite Scroll in jOOQ
  • Handling Virtual Threads
  • Debugging Streams and Collections

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: