Helidon: 2x productivity with Microprofile REST Client

We have already many articles before on how to write Microservice using the Helidon framework. We are going to further enhance our code with Microprofile REST Client to increase 2x productivity.

Microprofile REST Client overview

There are many frameworks available in the market to test REST APIs + automation. Microprofile introduced a new concept of REST Client by default. So that we can write type-safe REST Client while writing our code with few lines of code. This will speed up our API development and automation productivity in a great style.

Code example

The example code covers the following steps. For the sake of clarity, I have cut down the API implementation code. (Refer the previous article for implementation details)

  • Write REST API definition
  • A type-safe REST client implementation

REST API definition

public interface ICountryApi {
    Response all() throws DailyApiException; //----------> (1)

    Response get(@PathParam("code") String code) throws DailyApiException; //---> (2)

(1) -> To fetch all the available countries from the data source

(2) -> To get a particular country by the given id

A type-safe REST client implementation

@DisplayName("Country Api test cases")
public class CountryApiTest {
    static ICountryApi countryApi;
    static final String HOST = "http://localhost:8080"; //Microservice base Uri

    public static void initAll() throws URISyntaxException { 
        countryApi = RestClientBuilder.newBuilder()          //---> (1)
                .baseUri(new URI(HOST))
                .build(ICountryApi.class);                   //---> (2)

    @DisplayName("Method to test all() countries")
    public void testAll() throws DailyApiException {
        final Response response = countryApi.all();
        List<Country> countries = response.readEntity(
                new GenericType<List<Country>>() {});       //---> (3)
        assertEquals(2, countries.size(), "Result should be 2");

    @DisplayName("Method to test get() with proper country code")
    public void testGetWithProperId() throws DailyApiException {
        final Response response = countryApi.get("IND");
        assertEquals(200, response.getStatus(),
                "Status code should be 200");

        final Country country = response.readEntity(Country.class); //---> (4)
                () -> assertEquals("IND", country.getCode(),
                        "Country code should be IND"),
                () -> assertEquals("New Delhi", country.getCapital(),
                        "Capital should be New Delhi")

(1) -> Define a REST Client using the RestClientBuilder API introduced in Microprofile

(2) -> Use our ICountryApi to build REST API Client

(3) -> Deconstruct the Java bean directly from the HTTP response. Marshaling/ Un-marshalling will be taken care by the Helidon engine (Standard process for Microprofile implementation) [NOTE: I have included only 2 countries in the data store]

(4) Get the Country bean from the get() method and validate the response

I believe this would have given you clarity on writing a REST API client using the modern Microprofile REST Client specification. Deep dive into REST client specification to increase your productivity and speed up your automation coverage.

NOTE: Tests are written using the JUnit 5 framework.

{ Happy coding }

API Evangelist | Chief Hustler @workrituals.com | Habit Coach | Madras Java User Group Leader

Latest articles

When to use Firebase Cloud functions?

Recently I got a question from one of the Indie makers that, when to use Firebase Cloud Functions? This...

Jakarta Tech Talk: Build Microservice using Microprofile + Helidon

Recently I gave a presentation on Jakarta Tech Talk series on 'Building a Microservice using Microprofile + Helidon. Enjoy watching the presentation...

Serverless REST APIs made simple with Cloud functions

Introduction Serverless is one of the top keyword in the integration space right now. Everyone is talking about the...

How to get started with Google cloud functions?

The 'Cloud Functions' is a Google technology bundled as part of the Firebase platform. It is a serverless backend to support our...

Why Firebase is a great technology for ‘Indie Makers’...

Firebase is a technology developed by Google. In this article, I am going to explore why Firebase is a great technology for...

Similar articles