Helidon: 2x Productivity With Microprofile REST Client

DZone 's Guide to

Helidon: 2x Productivity With Microprofile REST Client

Learn how to use Helidon to increase productivity with MicroProfile REST Client overview and type-safe REST Client implementation.

· Microservices Zone ·
Free Resource

two people dancing

Make yourself for productive with Helidon.

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

You may also like: Improved Developer Experience and MicroProfile Integration for MicroProfile Integration for MicroProfile Rest Client 1.2

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

REST API Definition

@Path("/countries") @Produces(MediaType.APPLICATION_JSON) 
public interface ICountryApi { @GET Response all() 
  throws DailyApiException; 
//----------> (1) @Path("{code}") @GET 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 of by the Helidon engine (Standard process for Microprofile implementation).
    1. [ NOTE: I have included only two 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 specifications to increase your productivity and speed up your automation coverage.

NOTE: Tests are written using the JUnit 5 framework.

Happy coding!

Further Reading

MicroProfile: What You Need to Know

Invoking REST APIs From Java Microservices

Building Your Next Microservice With Eclipse MicroProfile (Updated)

code, helidon, microprofile, microservices, rest, rest api, rest client, type-safe

Published at DZone with permission of Thamizh Arasu , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}