Getting Started With Jakarta EE 10 - Jakarta REST

Photo of Luqman Saeed by Luqman Saeed

Jakarta EE 10 was released in September of 2022 as the first major release of the venerable Enterprise Java development platform since it was moved to the Eclipse Foundation. As a major release, it did come with a slew of updates to almost all the major specifications, including Jakarta REST. In this blog post, we take a look at how to get started developing RESTful web services on the Jakarta EE Platform using the Jakarta REST API.

The Setup

As a coreJakarta EE specification, there’s not much setup to be done in getting Jakarta REST up and running. Your typical Jakarta EE application will already contain a dependency on the platform in your dependency file, almost certainly the Maven pom.xml file. If you are not sure where or how to get started, check out my veryopinionated guide to getting started with Jakarta EE 10. 

With the dependency in place, we need to configure the root resource path for your REST resources. This path is where all your REST resources in the given will be accessed relative to. The HelloApplication class below shows a very bare-bones, fully functional root Jakarta REST configuration.


@ApplicationPath("/api")
public class HelloApplication extends Application {

}

This class extends the jakarta.ws.rs.core.Application class and is annotated with the jakarta.ws.rs.ApplicationPath annotation, passing in the “/api” string. This path, given the above configuration class, is the root path relative to which all REST resources created by this application will be accessed. The “/” preceding the root path is optional and will be prepended if omitted. 

The Application superclass extended above has three methods, getClasses, getSingletons and getProperties that you can override to further customise your configuration. The above configuration, however, will suffice for a large number of applications. With our Jakarta REST configuration in place, we are ready to create our obligatory, traditional “hello, world!” resource. 

The most atomic unit of a REST resource in Jakarta REST is a Java class that is annotated with a path via which it can be accessed. Within this class, Java methods can be exposed as REST resources through the use of annotations. The HelloResource class shown below puts these in together.



@Path("/hello-world")
public class HelloResource {

    @GET
    public String pint() {
        return "Hello, World!";
    }

}

The HelloResource is a plain old Java class annotated with the jakarta.ws.rs.Path annotation. This annotation is passed the value “hello-world”, meaning this class will be hosted at the url path /hello-world. The class has a single method, ping, that returns the string “Hello, World!”

The method is annotated with jakarta.ws.rs.GET annotation. This annotatation marks this method as responding to GET requests. In the abscence of any other metadata, a HTTP GET request to the path /hello-world will result in this method being beign called by the Jakarta REST runtime. The fully qualified URL to this class and method will be https://my-very-shiny-domain.wow/application-context-path/api/hello-world, broken down into your domain (localhost if you running locally), the context path being the path your Jakarta EE application is deployed to, then the configured root REST path and finally the hello-world path. A HTTP GET request to this path should return the string “Hello, World!” to the caller.

As a matured specification, you can develop all kinds of sophisticated REST applications using the Jakarta REST API. Check out the specification and take a look at some of the blogs and guides we have here on the Payara website to see how to develop modern stateless RESTful applications on the Jakarta EE Platform using Jakarta REST:

Related Posts

Comments