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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • User-Friendly API Publishing and Testing With Retrofit
  • Microservices With Apache Camel and Quarkus
  • Building a REST Service That Collects HTML Form Data Using Netbeans, Jersey, Apache Tomcat, and Java
  • How to Consume REST Web Service (GET/POST) in Java 11 or Above

Trending

  • Member Spotlight: Raghava Dittakavi
  • Using the Anthropic Evaluation Tool to Check the Claude Code Plugin Efficiency
  • Time to First Token: How Long Does It Take for a Large Model to Start Responding?
  • The SDLC After AI

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Mar. 15, 19 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
39.2K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • User-Friendly API Publishing and Testing With Retrofit
  • Microservices With Apache Camel and Quarkus
  • Building a REST Service That Collects HTML Form Data Using Netbeans, Jersey, Apache Tomcat, and Java
  • How to Consume REST Web Service (GET/POST) in Java 11 or Above

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook