Jersey is a REST framework that provides JAX-RS Reference Implementation and more. Jersey provides its own APIs that extend the JAX-RS toolkit with additional features and utilities to further simplify RESTful service and client development. It also exposes numerous extension SPIs so that developers may extend Jersey to best suit their needs. This article introduces the newly released Jersey 3 with a hello world project.
Requirements
Create a New Hello World Project
Step 1:
Create a new Maven project in IntelliJ Idea with the group id: com.javahelps.helloworld and artifact id: jersey-hello-world.
Step 2:
Add the following dependency management and dependencies to the pom.xml file. Though not related to Jersey, check out the final pom.xml for the Maven plugins to make an executable uber jar.
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-grizzly2-http</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
</dependency>
<!-- Additional dependency to support JSON -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-binding</artifactId>
</dependency>
After adding the dependencies, the pom.xml should look like this:
Not all newer versions of libraries guarantee backward compatibility with previous versions. Therefore, I recommend using the same version of the library as in the following pom.xml for your first run. Once you get the code working, search for the artifact ids in the public Maven Repository and use the latest version of the dependencies in your production code. Please note that older versions may have unpatched vulnerabilities and make your code vulnerable to attacks. I will try my best to keep the articles up to date but it is impossible for me to keep track of all libraries used in Java Helps. If you find any breaking changes in newer versions, please comment below.
<?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.javahelps.helloworld</groupId>
<artifactId>jersey-hello-world</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jersey.version>3.1.0</jersey.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-grizzly2-http</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
</dependency>
<!-- Additional dependency to support JSON -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-binding</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<inherited>true</inherited>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.4.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.javahelps.helloworld.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
pom.xml
Implement a Jersey Service
Step 1:
Create a new class named HelloService.java with the following code. I hope the code is self-explanatory.
package com.javahelps.helloworld;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.core.Response;
@Path("/sayhello")
public class HelloService {
@GET
@Path("/{name}")
public Response sayHello(@PathParam("name") String msg) {
String output = "Hello, " msg "!";
return Response.status(200).entity(output).build();
}
}
Step 2:
In the Main class, create a Grizzly server that can automatically scan and register all JAX-RS resources from the parent package.
package com.javahelps.helloworld;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;
import java.io.IOException;
import java.net.URI;
public class Main {
// Base URI the Grizzly HTTP server will listen on
private static final String BASE_URI = "http://localhost:8080/";
public static void main(String[] args) throws IOException {
// Create a resource config that scans for JAX-RS resources and providers in com.javahelps package
ResourceConfig config = new ResourceConfig().packages("com.javahelps");
// Create and start a grizzly http server
HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), config);
System.out.printf("Jersey app started with endpoints available at %s%nHit Ctrl-C to stop it...%n", BASE_URI);
// Wait for user input before shutting down the server
System.in.read();
// Shutdown the server
httpServer.shutdown();
}
}
Run the Jersey Service
After saving all the changes run the Main class from IntelliJ IDEA. By default, the web service will run on port 8080.
Using the Postman or CURL, send an HTTP request to any of the endpoints defined in the HelloService class. The following CURL command sends an HTTP GET request to the service.
curl http://localhost:8080/sayhello/JavaHelps
Just like MSF4J, these days Spring Boot shadows all different web service frameworks. However, choosing a JAX-RS implementation guarantees you can migrate to a different framework with less effort in case if you want. I hope this article gave you a good starting point to get your complex and production-ready Jersey service.
You can download the complete source code of this project along with all the resources from our Git Hub repository.
If you find this article useful, please share your thoughts below. If you have any questions or issues with getting Jersey 3 working, you can ask your questions in the comments. Java Helps community will try our best to answer your questions.