Introduction
In this tutorial, you will learn how to set up an REST service using Jakarta EE. You will create a simple web service with two endpoints. The first one will return a JSON array of Strings and the second one will return a JSON object.
Maven setup
To build our Helidon REST service with Maven, we need to edit the POM file. This file defines essential project information like its name, group ID, and version. More importantly, it specifies the Helidon framework as a parent project, inheriting its core functionalities. Additionally, the POM declares dependencies needed for our service to function, such as the helidon-microprofile bundle, and configures plugins for building and packaging the application.
|
|
This POM file sets up your Helidon project. It defines basic project details, includes necessary dependencies, and configures Maven for building your REST service. Let's now configure the beans for the project.
Discover beans
This XML file defines the “beans” used in our application. Think of beans like little building blocks.
Each bean represents a specific object in your program that can perform tasks or hold data. This file tells the application
which beans exist and how to find them. For the application we are building the class holding the REST endpoints is a bean for example.
The XML file needs to be placed in a specific location. Inside your project at the same level as the java directory create the following directories if you
don't have them already /resources/META-INF
. Inside the META-INF directory, you need to create a file called beans.xml with the
the following content:
|
|
This tells Helidon how it should find beans. In this case, it will look for annotated classes. Next, we'll dive into creating the REST endpoints.
Creating REST endpoints
REST endpoints are how we interact with this application. The purpose of an endpoint is to receive a request, process it, and return a response. In the following example, two endpoints are defined one will return a list of Frameworks, and the second returns an object instance.
|
|
These endpoints will by default return a JSON response, which means that your return object will be marshaled into a JSON object.
When calling the first endpoint using http://localhost:7001/frameworks/ will return:
|
|
The Second endpoint using this URL http://localhost:7001/frameworks/someData will turn the object instance into the following JSON:
|
|
With everything set up, it is time to see how to start your web service.
Starting the web server with a Main class
If you like to use your own main class you only need to create and start a server. You can start the server like this:
|
|
This will create and start a server. When you start this method you can try and access the endpoints you just created.
Using the build in Main class
You don't have to create your own Main class and method. You can also use the one that is part of Helidon. To use this class
you have to change the run configuration in your IDE to use io.helidon.microprofile.cdi.Main
. If you use Maven you already
configured this by adding the following to the pom.xml:
|
|
With the main class configured, you can open a shell and navigate to the project directory, and run the following command:
|
|
Clean will up the target directory if it already was created and package the code into a jar file.
With the jar file created, you can run it using the following command:
|
|
This will start the application after which you can access the REST endpoints you created earlier.
Conclusion
This post showed how to implement a REST service straightforwardly using Jakarta. This post is the first in a series of creating production-like application in small steps.
You can find the complete project here https://github.com/davidtos/RestEndpointWithJavaAndHelidon