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 Video Library
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
View Events Video Library
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

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • User-Friendly API Publishing and Testing With Retrofit
  • How to Develop Microservices With Spring Cloud and Netflix Discovery
  • Jakarta Security and REST in the Cloud: Part 2 Getting to Know the Basics
  • Jakarta Security and REST in the Cloud Part 1: Hello World

Trending

  • Demystifying Enterprise Integration Patterns: Bridging the Gap Between Systems
  • Unraveling Lombok's Code Design Pitfalls: Exploring Encapsulation Issues
  • Mastering Persistence: Why the Persistence Layer Is Crucial for Modern Java Applications
  • Microservices With Apache Camel and Quarkus (Part 5)
  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.97K 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.

Related

  • User-Friendly API Publishing and Testing With Retrofit
  • How to Develop Microservices With Spring Cloud and Netflix Discovery
  • Jakarta Security and REST in the Cloud: Part 2 Getting to Know the Basics
  • Jakarta Security and REST in the Cloud Part 1: Hello World

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

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: