A Simple Jakarta EE 9.1 REST Project
Tuesday, May 25, 2021 |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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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:
1
2
3
4
5
6
7
8
9
10
11
12
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$ 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! :)