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

Flutter: How does column layout work?

Layouts are the fundamental starting points for developing Flutter applications. There are many layout options are available by default. I am going...

Flutter: Stateless vs Stateful widgets

Flutter is a mobile application development framework developed by Google. You need to understand the fundamental building blocks before you dig deeper...

Why Flutter is a great technology for makers &...

As per the wikipedia, Flutter is an open source UI development kit created by Google. It is used to...

Why 12-Factory methodology matters to your Microservice development?

The 12-Factor application development methodology and why is it important for your microservices strategy

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...

Similar articles