Contents

Jakarta REST API with Java SE

Writen by: David Vlijmincx

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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
<dependencies>
    <dependency>
        <groupId>jakarta.ws.rs</groupId>
        <artifactId>jakarta.ws.rs-api</artifactId>
        <version>3.1.0</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-undertow</artifactId>
        <version>6.2.3.Final</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-json-binding-provider</artifactId>
        <version>6.2.3.Final</version>
    </dependency>
</dependencies>

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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
import jakarta.ws.rs.SeBootstrap;
import jakarta.ws.rs.core.Application;

public class Main extends Application{
    public static void main(String[] args) throws InterruptedException {

        Main main = new Main();

        SeBootstrap.Configuration config = SeBootstrap.Configuration.builder()
                .rootPath("my-app")
                .port(8080)
                .build();

        SeBootstrap.start(main, config);

        Thread.currentThread().join();
    }
}

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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

@Path("/")
public class RestResource {

    @GET
    @Produces({MediaType.APPLICATION_JSON})
    public Response HelloWorld(){
        return new Response("Hello, world!");
    }
    
}

The response class that is used in the previous example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
public class Response{
    private String message;

    public Response(String message) {
        this.message = message;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

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.

 




Questions, comments, concerns?

Have a question or comment about the content? Feel free to reach out!