Helidon: Microservice REST API

Writing REST API with the Helidon framework is not a hard job since it is compliant with Microprofile specification. The good part is that we don’t need to learn any new technology or annotation to achieve this. In this example below I am going to explain how to write REST API by following Helidon MP.

The code is as follows,

//Country.java
public class Country {
    private String code;
    private String name;
    private String capital;

    public Country() {}

    public Country(String code, String name, String capital) {
        this.code = code;
        this.name = name;
        this.capital = capital;
    }

    public String getCode() {
        return code;
    }

    public String getName() {
        return name;
    }

    public String getCapital() {
        return capital;
    }
}
//CountryManager.java
@ApplicationScoped
public class CountryManager {
    public List<Country> all() {
        return CollectionsHelper
                .listOf(
                        new Country("IND", "India", "New Delhi"),
                        new Country("AUS", "Australia", "Canberra")
                );
    }
}
//ICountryApi.java
@Path("/countries")
@Produces(MediaType.APPLICATION_JSON)
@ApplicationScoped
public interface ICountryApi {
    @GET
    Response all();
}
//CountryApi.java
@ApplicationScoped
public class CountryApi implements ICountryApi {
    @Inject
    CountryManager manager;

    @Override
    public Response all() {
        System.out.println("Enters Country api");
        return Response
                .ok(manager.all())
                .build();
    }
}
//Finally add an entry into the application
@ApplicationScoped
@ApplicationPath("/")
public class DailyApiApplication extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        return CollectionsHelper.setOf(
                CountryApi.class
        );
    }
}
Finally access the API using,
http://localhost:8080/countries

The code is so simple and straight forward. The entire code follows the annotation of JAX-RS specification which is part of our Microprofile specification. So the learning curve is nothing and no code is specific with Helidon. This is the beauty of Microprofile and the compliant frameworks. We can simply move this code to different Microprofile vendors based on our organization strategy changes. No code change is required to move between vendors.

{ Happy coding }

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

Latest articles

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

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

Similar articles