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.
Join the DZone community and get the full member experience.
Join For Freewe 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).
- write the rest api definition.
- a type-safe rest client implementation .
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) }
- to fetch all the available countries from the data source.
- 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
@beforeall
public static void initall() throws urisyntaxexception {
countryapi = restclientbuilder.newbuilder() //---> (1)
.baseuri(new uri(host))
.build(icountryapi.class); //---> (2)
}
@test
@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");
}
@test
@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)
assertall("country('ind')",
() -> assertequals("ind", country.getcode(),
"country code should be ind"),
() -> assertequals("new delhi", country.getcapital(),
"capital should be new delhi")
);
}
}
- define a rest client using the restclientbuilder api introduced in microprofile.
- use our icountryapi to build rest api client.
-
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).
- [ note: i have included only two countries in the data store].
- 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)
Published at DZone with permission of Thamizh Arasu, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Integration Architecture Guiding Principles, A Reference
-
Cypress Tutorial: A Comprehensive Guide With Examples and Best Practices
-
What Is mTLS? How To Implement It With Istio
-
Top 10 Pillars of Zero Trust Networks
Comments