Introduction
In this quick post, we will look at how to set up a REST resource using Jakarta's SeBootstrap
in a Java SE environment.
We will first look at the dependencies you will need and how to start the embedded server.
After that, we will create a REST endpoint that accepts HTTP GET calls.
Dependencies
To build the simple REST service, you need these dependencies inside your pom.xml:
|
|
Setup main class
Inside the main method, you set up the server. This is done in two steps: creating the configuration and starting the server. On line 9, we create a configuration for the server. We set the root path to my-app and the port to 8080. This means that you can access the server on http://localhost:8080/my-app.
The next step is to start the server as is done on line 14. To keep the server running, add a Thread.currentThread().join();
.
Adding the .join
prevents the application from immediately exiting.
|
|
Creating a resource
Next up is creating a REST resource for your application. The following example creates an endpoint at /
, meaning you can access it
by going to this URL: http://localhost:8080/my-app.
On line 11 is a method annotated with @GET
. This means that when you do a GET request to http://localhost:8080/my-app, this method is executed.
The method creates a response object which is a simple class with a String property called message. The method is also annotated with @Produces({MediaType.APPLICATION_JSON})
which means that the object returned by this method is converted to JSON.
|
|
The response class that is used in the previous example:
|
|
Conclusion
This post showed how to implement and configure a REST server using Jakarta's SeBootstrap. We configured the server and created an endpoint you use.