Coming Up for Air

A Simple Jakarta EE 9.1 REST Project

Jakarta EE 9.1 was released today, which now lets developers use — officially — Java 11 with the shiny new Jakarta EE namespace introduce in EE 9. So what does a simple Jakarta EE 9.1 REST project look like? I’m so glad you asked. :)

Let’s start with the Maven POM:

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.steeplesoft</groupId>
    <artifactId>jakarta-ee-91-demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>jakarta.platform</groupId>
                <artifactId>jakarta.jakartaee-bom</artifactId>
                <version>9.1.0</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>jakarta.ws.rs</groupId>
            <artifactId>jakarta.ws.rs-api</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>

Strictly speaking, you don’t need the BOM import, but it does make adding Jakarta EE deps easier later. We’ll need two classes, an Application instance, and at least one endpoint. Notice the imported packages. ;)

The Jakarta REST application:

JakartaEEApplication.java
import java.util.HashSet;
import java.util.Set;

import jakarta.ws.rs.ApplicationPath;
import jakarta.ws.rs.core.Application;

@ApplicationPath("/")
public class JakartaEEApplication extends Application {
    @Override
    public Set<Class<?>> getClasses() {
        final Set<Class<?>> classes = new HashSet<>(1);
        classes.add(RestEndpoint1.class);
        return classes;
    }
}

And the endpoint:

RestEndpoint.java
package com.steeplesoft.ee91;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;

@Path("/endpoint1")
public class RestEndpoint1 {
    @GET
    public String getString() {
        return "Hello World, from Jakarta EE 9.1!";
    }
}

And now let’s deploy this on WildFly Preview:

$ java --version
openjdk 11.0.11 2021-04-20
OpenJDK Runtime Environment AdoptOpenJDK-11.0.11+9 (build 11.0.11+9)
OpenJDK 64-Bit Server VM AdoptOpenJDK-11.0.11+9 (build 11.0.11+9, mixed mode)
$ wget https://download.jboss.org/wildfly/23.0.2.Final/wildfly-preview-23.0.2.Final.zip
$ unzip wildfly-preview-23.0.2.Final.zip
$ wildfly-preview-23.0.2.Final/bin/standalone.sh
...
(In another window)
$ wildfly-preview-23.0.2.Final/bin/jboss-cli.sh -c "deploy target/jakarta-ee-91-demo-1.0-SNAPSHOT.war"
$ http :8080/jakarta-ee-91-demo-1.0-SNAPSHOT/endpoint1
HTTP/1.1 200 OK
Connection: keep-alive
Content-Length: 33
Content-Type: application/octet-stream
Date: Tue, 25 May 2021 20:18:03 GMT

Hello World, from Jakarta EE 9.1!

That’s it. Easy peasy. Go grab the bits while they’re warm! :)

Quotes

Sample quote

Quote source