Building a Simple Java Microservice Using Helidon MicroProfile APIs and JPA

This post talks about building a simple data bound microservice using Helidon framework. If you are new to Helidon, please take a step back and read the details in the official site :

Let us get our hands dirty to get a feel of the offerings in Helidon framework.

Setting up the Environment

First step is to ensure you have installed all necessary tools required for building and running this example.

The above two are enough to build and run the service discussed in this post locally. However if you want to make your service cloud ready, please install r Docker, Kubectl and optionally Minikube as discussed in the following link:

Building a Simple Microservice with Helidon MicroProfile

Helidon lets you to build microservices in two flavors( programming model ):

  1. Helidon SE : With this approach, you will use vanilla Helidon WebServer, Config and Security APIs directly for building the desired services. These APIs follow functional programming style. This gives you more control on the execution of you code. However, this approach locks down the service implementation to Helidon and in future it will be very hard to port the implementation to different microservice platform such as Payara Micro.
  2. Helidon MP : With this approach, you will use Helidon MicroProfile(MP) APIs for building the services. MicroProfile specifies a collection of Java EE APIs such as CDI, JAX-RS for building portable Java microservices across various ‘microprofile’ ( complaint platforms.

In this post we will use Helidon MP programming model for building the microservice. Let us get the ball moving.

  1. Generate the project with maven archetype
mvn archetype:generate -DinteractiveMode=false \
-DarchetypeGroupId=io.helidon.archetypes \
-DarchetypeArtifactId=helidon-quickstart-mp \
-DarchetypeVersion=0.10.5 \
-DgroupId=com.jobinesh.helidon.examples \
-DartifactId=dept-service-mp \

2. Next is to set up a database for use in our example. You are free to choose database of your choice. This example uses a a very lightweight and simple database — SQLite. To learn more about SQLite, visit the following link: SQLite is included in macOS and Mac OS X by default.

3. Define a Departments table in SQLite database.

CREATE TABLE departments
( department_id NUMBER(4)
, department_name VARCHAR2(30)
, manager_id NUMBER(6)
, location_id NUMBER(4)
) ;
ON departments (department_id) ;

4. As next step, we need to add necessary Java code in our example in order to read from/write to Department table. We will use Java Persistence API (JPA) for reading and writing data between Java objects and SQLite. As the source that we generated in Step 1 is not configured to use JPA, let us add that support.

5. Go to the generated project source and all required dependencies in the pom.xml. Here is the list of dependencies added for this example:

<!-- SQLite driver --> 

6. Add src/main/resources/META-INF/persistence.xml to the project. The persistence.xml file is a standard configuration file in JPA and it has to be included in the META-INF directory inside the JAR file that contains the entity beans. Here is the persistence.xml used for this example

<?xml version=”1.0" encoding=”UTF-8"?>
<persistence version=”2.1" xmlns=”" xmlns:xsi=”" xsi:schemaLocation=”">
<persistence-unit name=”demo-pu” transaction-type=”RESOURCE_LOCAL”>
<property name=”javax.persistence.jdbc.driver”
<property name=”javax.persistence.jdbc.url”

7. We are done with all basic configuration. Now you can add the JPA entities to the project source as appropriate. In this simple example we use only one entity, namely Departments. Here is the Departments JPA entity defined for Departments table:

8. Next steps is to build Restful resource class which will act as REST end points. We use JAX-RS APIs for building REST resource class. You can use JPA entities that you created in previous step in this class to read or write data from database while building REST APIs. The DepartmentResource class used in this example is available here:

9. Add the unit test cases as appropriate. Here is a basic example:

10. To run this example, open terminal window, goto the root of the project and do the following :

mvn package
java -jar target/dept-service-mp.jar

11. To try out the REST APIs offered in this example, you and use curl utility and try the following:

1. Create a department 
curl -X POST -H "Content-Type: application/json" \
--data '{"departmentId":30,"departmentName":"Finance","locationId":1700,"managerId":200}' \
2. List all departments
curl -X GET http://localhost:8080/departments
3. Delete a department
curl -X "DELETE" http://localhost:8080/departments/30

The complete source code used for this example is available in the following git repository :

Congrats, we have finished building a simple microservice ! This is a simple example, primarily meant for illustrating the basics and usage pattern of data bound JAX-RS resource class when used along with Helidon.

What next ?

If you are keen on learning more on this topic, please check out this post:

The views expressed on this post are my own and do not necessarily reflect the views of my employer.