Consuming an API in Java

Jan 2023

dev

In this tutorial, we will be building a Spring Boot Application that consumes a web API. The application consumes the API and returns a JSON response. The application is a RESTFUL API itself as requests made to the application will return the JSON object that it initially consumed. The sample API being consumed today is a list of FX rates against USD. This can be found here


Spring Boot


Spring Boot allows you create stand-alone Spring applications. There are many ways to start a Spring Boot Application. For this project, I used the Spring Intializr which allows you fill in your project details, pick out your options and download a bundled up project as a zip file

Let's create the model


The model is a representation of the data we are going to be receiving. We need to create these data models (java classes) so we can store the JSON received as POJO (Plain Old Java Objects) By looking at the JSON data, we have an idea of the schema. I created Root and Rates data models to look like this


package com.example.consumingrest;
import com.fasterxml.jackson.annotation.JsonProperty;

public class Root{
@JsonProperty("provider")
public String getProvider() {
return this.provider; }
public void setProvider(String provider) {
this.provider = provider; }
String provider;

@JsonProperty("WARNING_UPGRADE_TO_V6")
public String getWARNING_UPGRADE_TO_V6() {
return this.wARNING_UPGRADE_TO_V6; }
public void setWARNING_UPGRADE_TO_V6(String wARNING_UPGRADE_TO_V6) {
this.wARNING_UPGRADE_TO_V6 = wARNING_UPGRADE_TO_V6; }
String wARNING_UPGRADE_TO_V6;

@JsonProperty("terms")
public String getTerms() {
return this.terms; }
public void setTerms(String terms) {
this.terms = terms; }
String terms;

@JsonProperty("base")
public String getBase() {
return this.base; }
public void setBase(String base) {
this.base = base; }
String base;
...


This Class allows us capture the higher layer properties like terms, date and time_last_updated. The deeper nested properties like that exchange rates themselves, will be captured in this model below



public class Rates{

@JsonProperty("USD")
public int getUSD() {
return this.uSD; }
public void setUSD(int uSD) {
this.uSD = uSD; }
int uSD;

@JsonProperty("AED")
public double getAED() {
return this.aED; }
public void setAED(double aED) {
this.aED = aED; }
double aED;

@JsonProperty("AFN")
public double getAFN() {
return this.aFN; }
public void setAFN(double aFN) {
this.aFN = aFN; }
double aFN;


We can now assume we have converted all JSON data to POJO format

Controller Class


We will be using WebClient to hold and consume the external REST service URI.



@RestController public class MyController {

private final String FX_API = "https://api.exchangerate-api.com/v4/latest/USD";

@Autowired
private WebClient.Builder webClientBuilder;

@GetMapping("/rates")
public Root getFx() {
return webClientBuilder.build()
.get()
.uri(FX_API)
.retrieve().bodyToMono(Root.class)
.block();
} }


FX_API holds the URI we would want to consume. WebClient.Builder is a mutable builder for creating a WebClient. We pass the link through .uri() and retrieve the response using .retrieve() We also pass the top level class Root to .bodyToMono() Notice the GetMapping annotation just before the getFx() method

Application Class


@SpringBootApplication @ComponentScan("com.example")
public class ConsumingRestApplication { @Bean

public WebClient.Builder webClientBuilder() {
return WebClient.builder();
}

public static void main(String[] args) {
SpringApplication.run(ConsumingRestApplication.class, args); }
}

The ConsumingRestApplication class is used to run the application. It also returns the bean of WebClient.Builder I use ./mvnw spring-boot:run to run the application


Test it

All that is left to do now is to test our application. To test the application, use Postman and hit the below endpoint http://localhost:8080/rates

Using GET HTTP method. You should now have a REST call be made to an external API and the data mapped to our models

» More posts

Let's Connect

You can offer to hire me, collaborate with me, check up on me

Copyright 2020 Ini Olorunnishola olo-ini