To create a RESTful web service using Jakarta EE, we will begin by
summarizing what we want to build.
We will build a service that will accept an HTTP GET1
request at http://localhost:8080/restfulservice/hello.
It will respond with the following JSON payload, as the following listing
shows:
{"text":"Hello from Jakarta EE"}
We can then further customize and improve it according to our needs.
OK, now that we have specified our requirements, you will need to follow these
steps:
Set up your development environment:
Install a Java Development Kit (JDK). Please make sure you have Java SE 8 or
higher (we have tested with Java SE 8, Java SE 11 and Java SE 17). You can
choose any vendor distribution of your choice from Adoptium
as well.
Install an application server that supports Jakarta EE. Download any of the
Jakarta EE compatible products.
To install all of these, we can use the SDKMan. We can go
through the steps mentioned in the how-to guide.
How to complete this guide
In this getting started guide, you may start using Eclipse Starter for Jakarta
EE, finish each step, or skip fundamental setup stages that you already know.
You can also begin with an IDE or choose a project structure from well-known Maven archetypes.
Create a new project with Eclipse Starter for Jakarta EE
To use Eclipse Starter for Jakarta EE, we need to take the following steps:
Navigate to start.jakarta.ee. This service will set up all the essential
dependencies for an application. The current version of the Starter only
supports Maven. In the future, we may be able to choose between Gradle and
Maven.
Select the desired version of Jakarta EE from the available options. As of
now, the options include Jakarta EE 8, Jakarta 9.1, and Jakarta 10. You may
choose the Jakarta EE profile from Platform, Core, or Web. However, for most
options, you may safely stick with the defaults.
Then specify the Group, Artifact and Version for your new project.
Once you have done this, the following box will let you copy a command. Copy
the command, open your terminal, paste it, and run it.
In this code structure, along with other classes and configurations, we are
interested in two classes in particular: RestResource.java and HelloRecord.java.
Let’s open the RestResource.java file first.
packageorg.eclipse.restfulservice.resources;importjakarta.ws.rs.GET;importjakarta.ws.rs.core.MediaType;importjakarta.ws.rs.Path;importjakarta.ws.rs.Produces;@Path("hello")publicclassRestResource{@GET@Produces(MediaType.APPLICATION_JSON)publicHelloRecordhello(){returnnewHelloRecord("Hello from Jakarta EE");}}
This defines a RESTful web service that returns a JSON representation of a RestResource when a GET request is made to the "/hello" endpoint.
The jakarta.ws.rs.Path annotation establishes a connection between the URL
given by the user and the Java class responsible for handling the request. The jakarta.ws.rs.GET annotation tells us we must use the HTTP GET method to
access our endpoint. The jakarta.ws.rs.Produces annotation allows you to
specify the format of the response. In our case, it will produce a
JSON2
response by converting the
object of HelloRecord.
The method hello() is defined to return a HelloRecord. This is the new record class that was released in Java 16.
The project structure was generated without having a runtime associated with
it. The merit of it is that we can choose from a multitude of runtimes. A list
of compatible Jakarta EE can be found here.
We will use WildFly in this tutorial.
To do that, we need to add an additional plugin to the pom.xml file.
Include the above plugin in the plugins sections of the pom.xml.
The wildfly-maven-plugin is used to deploy, redeploy, undeploy or run the
Jakarta EE application. There is also the ability to execute CLI commands. The
whole configuration of this plugin can be found here: WildFly Maven Plugin – Introduction.
There are multiple ways you can configure a local instance of WildFly. Some
configurations can be found here: WildFly Maven Plugin – Run Example.
However, in this tutorial, we can leave all the configurations as they are, as
we will use the default, and that’s enough for us.
In particular, we are interested in wildfly:run CLI command. Let’s run the
following command from the command line:
mvn clean package wildfly:run
The command above will build the app and put it on Wildfly. If Wildfly is not
installed, it will download and run it, and then the war file will be deployed.
Once the application runs, we will get the following output in the terminal:
[INFO] Scanning for projects...
[INFO][INFO] ------------------in< org.eclipse:restfulservice >---------------------
[INFO] Building restfulservice 1.0.0-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[INFO].....
skipped the log for brevity
.....
03:35:02,323 INFO [org.jboss.resteasy.resteasy_jaxrs.i18n](ServerService Thread Pool -- 32) RESTEASY002225: Deploying jakarta.ws.rs.core.Application: class org.eclipse.restfulservice.ApplicationConfig
03:35:02,346 INFO [org.wildfly.extension.undertow](ServerService Thread Pool -- 32) WFLYUT0021: Registered web context: '/restfulservice'for server 'default-server'03:35:02,365 INFO [org.jboss.as.server](management-handler-thread - 1) WFLYSRV0010: Deployed "restfulservice.war"(runtime-name : "restfulservice.war")
Hostname: the name of the machine on which WildFly Server is installed.
Port: The WildFly Server’s listening port for incoming HTTP requests. This is
port 8080 by default, but it can be configured.
Context-root: The context root to which the deployed application has been
assigned. By default, this is the filename (without the extension) of the WAR
file that is being deployed. But it can be changed when the file is being
deployed.
REST-config: The value we have defined for the @ApplicationPath
annotation in our project. By default, it is empty, which is indicated simply
by /. We can easily configure it in our ApplicationConfig class.
Resource-config: the value defined in the @Path annotation on the Java
class defines an endpoint. In our case, "/hello" is handled by the RestResource class.
If we want to change the REST-config part, for example, to "/api", we can
change the value in the @ApplicationPath annotation like this:
Congratulations! You have just learned how to develop a RESTful web service
using Jakarta EE.
HTTP GET is a request method supported by HTTP used by the World Wide
Web. It requests a representation of the specified resource. Its general
form is: GET /path/to/resource HTTP/1.1
JSON - It stands for JavaScript Object Notation. JSON is a text format
for storing and transporting data