Java - Building REST APIs Using Jakarta EE
Building REST APIs using Jakarta EE is a common approach for creating enterprise-level web services in Java. Jakarta EE, formerly known as Java EE, provides a set of specifications and APIs that help developers build secure, scalable, and maintainable applications. REST APIs created with Jakarta EE are widely used in web applications, mobile backends, cloud services, and enterprise systems because they follow standardized methods for communication over HTTP.
A REST API, or Representational State Transfer Application Programming Interface, allows applications to exchange data through HTTP requests such as GET, POST, PUT, and DELETE. Jakarta EE simplifies the creation of these APIs by offering built-in technologies like JAX-RS for RESTful web services, CDI for dependency injection, and JSON-B for JSON data binding. These technologies work together to reduce boilerplate code and improve development efficiency.
Introduction to JAX-RS
JAX-RS is the Jakarta EE specification used to create RESTful web services. It provides annotations that define endpoints, HTTP methods, request parameters, and response formats.
A simple REST API resource class looks like this:
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
@Path("/hello")
public class HelloResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String sayHello() {
return "Hello from Jakarta EE";
}
}
In this example:
-
@Path("/hello")defines the endpoint URL. -
@GETindicates that the method handles HTTP GET requests. -
@Produces(MediaType.TEXT_PLAIN)specifies the response format.
When the application runs, accessing /hello returns the text response.
Setting Up a Jakarta EE Project
To build REST APIs, developers usually use tools like Maven or Gradle along with an application server such as:
-
GlassFish
-
Payara
-
WildFly
-
Open Liberty
A Maven dependency for JAX-RS may look like this:
<dependency>
<groupId>jakarta.platform</groupId>
<artifactId>jakarta.jakartaee-api</artifactId>
<version>10.0.0</version>
<scope>provided</scope>
</dependency>
The application also requires a configuration class:
import jakarta.ws.rs.ApplicationPath;
import jakarta.ws.rs.core.Application;
@ApplicationPath("/api")
public class MyApplication extends Application {
}
This sets the base path /api for all REST endpoints.
Handling Different HTTP Methods
REST APIs commonly support CRUD operations:
-
Create → POST
-
Read → GET
-
Update → PUT
-
Delete → DELETE
Example:
@Path("/users")
public class UserResource {
@GET
public String getUsers() {
return "Fetching users";
}
@POST
public String createUser() {
return "User created";
}
@PUT
public String updateUser() {
return "User updated";
}
@DELETE
public String deleteUser() {
return "User deleted";
}
}
Each method responds to a specific HTTP request type.
Using Path Parameters
Path parameters allow dynamic values in URLs.
Example:
@GET
@Path("/{id}")
public String getUser(@PathParam("id") int id) {
return "User ID: " + id;
}
If the request URL is:
/api/users/5
The value 5 is passed into the method.
Working with JSON Data
Modern REST APIs commonly use JSON for data exchange. Jakarta EE supports JSON through JSON-B and JSON-P.
Example Java class:
public class User {
private int id;
private String name;
public User() {
}
public User(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
REST endpoint:
@GET
@Produces(MediaType.APPLICATION_JSON)
public User getUser() {
return new User(1, "Rahul");
}
The response becomes:
{
"id": 1,
"name": "Rahul"
}
Jakarta EE automatically converts Java objects into JSON format.
Consuming Request Data
REST APIs often receive data from clients.
Example:
@POST
@Consumes(MediaType.APPLICATION_JSON)
public String addUser(User user) {
return "Received user: " + user.getName();
}
The API can receive JSON input like:
{
"id": 2,
"name": "Anita"
}
The framework converts the JSON data into a Java object automatically.
Dependency Injection with CDI
CDI, or Contexts and Dependency Injection, is another important Jakarta EE feature. It allows objects to be injected automatically.
Example:
import jakarta.inject.Inject;
@Path("/service")
public class ServiceResource {
@Inject
UserService userService;
}
Benefits include:
-
Reduced object creation code
-
Better modularity
-
Easier testing
-
Improved maintainability
Exception Handling
REST APIs should return meaningful error responses.
Example:
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.Response;
throw new WebApplicationException(
"User not found",
Response.Status.NOT_FOUND
);
This returns an HTTP 404 error with a message.
Custom exception mappers can also be created for centralized error handling.
Securing REST APIs
Security is critical for enterprise APIs. Jakarta EE supports authentication and authorization through:
-
JWT authentication
-
Role-based security
-
OAuth integration
-
HTTPS communication
Example:
@RolesAllowed("admin")
@DELETE
@Path("/{id}")
public void deleteUser(@PathParam("id") int id) {
}
Only users with the admin role can access this endpoint.
Database Integration
Jakarta EE integrates with databases using JPA (Jakarta Persistence API).
Entity example:
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
@Entity
public class Product {
@Id
private int id;
private String name;
}
Developers can use repositories or EntityManager to perform database operations.
Advantages of Jakarta EE for REST APIs
-
Standardized enterprise framework
Jakarta EE follows official specifications that ensure consistency across applications. -
Scalability
It supports large enterprise systems and distributed applications. -
Built-in security
Authentication and authorization are integrated into the framework. -
Simplified development
Annotations and dependency injection reduce boilerplate code. -
Vendor independence
Applications can run on multiple Jakarta EE servers without major code changes.
Common Use Cases
Jakarta EE REST APIs are commonly used in:
-
Banking systems
-
E-commerce platforms
-
Healthcare applications
-
Cloud services
-
Enterprise dashboards
-
Mobile application backends
These APIs allow different systems to communicate efficiently using standard web protocols.
Conclusion
Building REST APIs using Jakarta EE provides a powerful and standardized way to create enterprise-grade web services in Java. By using technologies like JAX-RS, CDI, JSON-B, and JPA, developers can create scalable, secure, and maintainable APIs with less effort. Jakarta EE simplifies many backend development tasks while ensuring high performance and compatibility across enterprise environments. As modern applications increasingly rely on APIs for communication, learning Jakarta EE REST development becomes an important skill for Java developers working in enterprise and cloud-based systems.