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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
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

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

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

  • AI Agents: A New Era for Integration Professionals
  • The Evolution of Scalable and Resilient Container Infrastructure
  • Infrastructure as Code (IaC) Beyond the Basics
  • Driving DevOps With Smart, Scalable Testing
  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.

By 
Niklas Heidloff user avatar
Niklas Heidloff
DZone Core CORE ·
Mar. 15, 19 · Tutorial
Likes (7)
Comment
Save
Tweet
Share
38.6K 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

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • 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:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!