Building a REST API Development with Jersey

As a developer constantly looking for ways to improve my skills, I recently decided to dive into REST API development using Jersey, a popular framework for building RESTful web services in Java. In this blog, I will walk through my experience of setting up a Jersey project, creating a resource class, and ensuring data is returned in JSON format.

Setting Up the Jersey Project

I started by setting up a Maven-based Jersey project. Jersey provides an easy way to build RESTful APIs with minimal configuration. Here’s how I initialized my project:

  1. Created a Maven Project – I set up a simple Maven project with the necessary dependencies.
  2. Added Jersey Dependencies – The following dependencies were added to the pom.xml file:
<dependencies>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-common</artifactId>
        <version>3.0.2</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>3.0.2</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>3.0.2</version>
    </dependency>
</dependencies>

These dependencies allow Jersey to process JSON requests and responses using Jackson.

Creating the Resource Class

Once the project was set up, I created a resource class to expose API endpoints. I wanted to create a simple API that returns user data in JSON format.

Here’s my UserResource.java class:

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import java.util.Arrays;
import java.util.List;

@Path("/users")
public class UserResource {

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<User> getUsers() {
        return Arrays.asList(
            new User(1, "John Doe", "johndoe@example.com"),
            new User(2, "Jane Smith", "janesmith@example.com")
        );
    }
}

In this class:

  • The @Path("/users") annotation defines the endpoint URL as /users.
  • The @GET annotation makes this method accessible via HTTP GET requests.
  • The @Produces(MediaType.APPLICATION_JSON) annotation ensures that the data is returned in JSON format.
  • The method returns a list of users using a simple User model.

Creating the User Model

To represent user data, I created a User.java model:

public class User {
    private int id;
    private String name;
    private String email;

    public User(int id, String name, String email) {
        this.id = id;
        this.name = name;
        this.email = email;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getEmail() {
        return email;
    }
}

This simple POJO (Plain Old Java Object) contains three attributes: id, name, and email. The constructor and getters allow Jersey to serialize the object into JSON automatically.

Deploying and Testing the API

To test my API, I deployed the project on a servlet container like Apache Tomcat and accessed the endpoint via a browser or Postman:

http://localhost:8080/myapp/users

The response was as expected:

[
  {"id":1,"name":"John Doe","email":"johndoe@example.com"},
  {"id":2,"name":"Jane Smith","email":"janesmith@example.com"}
]

Final Thoughts

This project gave me a hands-on understanding of REST API development with Jersey. Setting up the environment, defining resources, and ensuring proper JSON responses were valuable learning experiences. Jersey simplifies the process of building RESTful services and integrates well with other Java frameworks.

In the future, I plan to explore additional features like handling HTTP POST requests, implementing authentication, and integrating a database for persistent storage. If you’re getting started with REST API development in Java, Jersey is an excellent framework to work with!

Related blog posts