Build a RESTful Web Service with Jakarta EE – Step‑by‑Step Guide
This tutorial walks you through setting up the JDK, Maven, and a Jakarta EE‑compatible server, generating a project with the Eclipse Starter, writing a simple REST endpoint, configuring the WildFly Maven plugin, and testing the service via a browser or curl, all with concrete code examples.
Set Up Development Environment
Install a Java Development Kit (JDK) 8 or higher (the example uses 8; testing can be done with 11 or 17). Install a Jakarta EE‑compatible application server. Install Maven 3+ (or use SDKMan).
Create a RESTful Web Service Project
Generate the project with the Jakarta EE Eclipse Starter, an IDE, or a Maven archetype. The following Maven command creates a minimal Jakarta EE 10 project:
mvn archetype:generate -DarchetypeGroupId=org.eclipse.starter -DarchetypeArtifactId=jakartaee10-minimal -DarchetypeVersion=1.1.0 -DgroupId=org.eclipse.rest -DartifactId=rest-service -Dprofile=web-api -Dversion=1.0.0-SNAPSHOT -DinteractiveMode=falseThe command produces the following directory layout:
.
├─ pom.xml
└─ src
├─ main
│ └─ java
│ └─ org
│ └─ eclipse
│ └─ restfulservice
│ ├─ ApplicationConfig.java
│ └─ resources
│ ├─ HelloRecord.java
│ └─ RestResource.java
├─ resources
│ └─ META-INF
│ └─ beans.xml
└─ webappRestResource.java
package org.eclipse.restfulservice.resources;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
@Path("hello")
public class RestResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
public HelloRecord hello() {
return new HelloRecord("Hello from Jakarta EE");
}
}The @Path("hello") annotation binds the class to the URI /hello. @GET restricts handling to HTTP GET requests, and @Produces(MediaType.APPLICATION_JSON) tells Jakarta REST to serialize the returned object as JSON.
HelloRecord.java (record version)
package org.eclipse.restfulservice.resources;
public record HelloRecord(String text) {}If you are on a Java version prior to 16, replace the record with a traditional POJO:
package org.eclipse.restfulservice.resources;
public final class HelloRecord {
private final String text;
public HelloRecord(String text) {
this.text = text;
}
public String text() {
return text;
}
}Run the Project from the CLI
The generated project does not include a runtime container, allowing you to choose any Jakarta EE‑compatible server. This guide uses WildFly.
Add the WildFly Maven plugin to pom.xml:
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<version>2.1.0.Beta1</version>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>deploy</goal>
</goals>
</execution>
</executions>
</plugin>The plugin deploys, redeploys, or runs Jakarta EE applications on WildFly. Build and start the server with: mvn clean package wildfly:run If WildFly is not installed locally, the plugin downloads and launches it, then deploys the generated WAR file.
Test the Service
With the server running, request the endpoint:
curl -v http://localhost:8080/restfulservice/helloThe response is the JSON payload:
{"text":"Hello from Jakarta EE"}URL Structure Overview
http://<hostname>:<port>/<context-root>/<REST-config>/<resource-config>Hostname : the machine on which WildFly is installed.
Port : HTTP port (default 8080, configurable).
Context‑root : the WAR file name without extension (default).
REST‑config : the value of @ApplicationPath (default “/”).
Resource‑config : the value of @Path on the resource class (e.g., “/hello”).
To change the REST base path, modify ApplicationConfig.java:
import jakarta.ws.rs.ApplicationPath;
import jakarta.ws.rs.core.Application;
@ApplicationPath("/api")
public class ApplicationConfig extends Application {}After rebuilding and restarting, the endpoint becomes http://localhost:8080/restfulservice/api/hello.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
JakartaEE China Community
JakartaEE China Community, official website: jakarta.ee/zh/community/china; gitee.com/jakarta-ee-china; space.bilibili.com/518946941; reply "Join group" to get QR code
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
