Helidon Web Client Example to call REST API

In this tutorial, learn how we can call APIs using the Helidon Web Client. You might be familiar with using Apache HTTP Client to call APIs, but Helidon also has the functionality to call APIs with its easy-to-use web client builder.

Helidon Web Client Example

In this example, we will try to get the list of countries from https://restcountries.com/ using Helidon. The response is in JSON format and for simplicity, our POJO contains minimal data.

First, let’s add the necessary dependencies. In your pom.xml, add the following lines:

        
            io.helidon.webclient
            helidon-webclient
            ${helidon.version}
        

        
            io.helidon.media
            helidon-media-jackson
            ${helidon.version}
        

Here’s our complete pom.xml for reference:



    4.0.0

    com.javapointers
    helidon-web-client
    1.0-SNAPSHOT

    
        11
        11
        2.4.0
    

    
        
            io.helidon.webclient
            helidon-webclient
            ${helidon.version}
        

        
            io.helidon.media
            helidon-media-jackson
            ${helidon.version}
        

        
            org.projectlombok
            lombok
            1.18.4
        
    

Next, let’s create the POJOs for the response. Here, we will just create the classes for Country and Name. Take note that we add the Jackson annotation @JsonIgnoreProperties(ignoreUnknown = true) since we are not capturing all the response fields.

Country.java

package com.javapointers.helidon.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;

import java.util.List;

@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class Country {

    private Name name;
    private List<String> tld;
    private boolean independent;
    private String status;
    private boolean unMember;
    private List<String> capital;
    private String region;
    private String subregion;

}

Name.java

package com.javapointers.helidon.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;

@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class Name {

    private String common;
    private String official;

}

Next, we’ll create the main method containing the logic to create the HTTP client. In the code below, you will see that we add the reader and writer support using JacksonSupport class. This will make our client automatically read or write JSON requests/responses so we don’t need to manually convert JSON strings to a java object.

package com.javapointers.helidon;

import com.javapointers.helidon.model.Country;
import io.helidon.common.GenericType;
import io.helidon.common.http.Http;
import io.helidon.common.http.MediaType;
import io.helidon.common.reactive.Single;
import io.helidon.media.jackson.JacksonSupport;
import io.helidon.webclient.WebClient;

import java.util.List;
import java.util.concurrent.ExecutionException;

public class TestWebClient {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        WebClient webClient = WebClient.builder()
                .baseUri("https://restcountries.com")
                .addHeader(Http.Header.ACCEPT_LANGUAGE, "en-us")
                .addHeader(Http.Header.ACCEPT_ENCODING, "gzip, deflate, br")
                .addReader(JacksonSupport.reader())
                .addWriter(JacksonSupport.writer())
                .build();

        Single<List<Country>> request = webClient.get()
                .path("v3.1/all")
                .contentType(MediaType.APPLICATION_JSON)
                .request(new GenericType<>() {});

        List<Country> listOfCountries = request.get();

        System.out.println(listOfCountries);
    }
}

You will also notice that in the request method, we add the GenericType. If we only expect a single JSON object, then we can just directly add the class name e.g. Country.class. But since we expect it to be a list, we want our client to automatically convert those lists of JSON countries into a list of Country objects in Java.

Running the code will retrieve the list of countries and automatically converts it to a list of Java object. Here’s a screenshot from debug mode:

helidon web client example response in debug

As you can see from the above screenshot, the JSON strings were automatically converted into Country objects using the internal Jackson Mapper in Helidon.

Share this tutorial!