Close Menu
Java HelpsJava Helps
    What's Hot

    Things To Do After Installing IntelliJ IDEA

    December 20, 2022

    How to Set Proxy for IntelliJ IDEA?

    December 20, 2022

    What is Garbage Collection?

    December 7, 2022
    Facebook X (Twitter) Instagram
    Java HelpsJava Helps
    • Home
    • How To
      1. Install
      2. Run
      3. Configure
      4. View All

      How to Install MySQL with phpMyAdmin on Ubuntu?

      December 4, 2022

      Install The Latest IntelliJ IDEA on Linux

      November 28, 2022

      Install The Latest Apache Maven on Linux

      November 27, 2022

      Install The Latest Oracle JDK on Linux

      November 27, 2022

      Run NiFi Cluster in Docker with SSL Enabled

      December 6, 2022

      How to Run Apache NiFi Docker on Mac M1?

      December 1, 2022

      How to Run Apache NiFi on Docker?

      December 1, 2022

      Create A New Maven Project In IntelliJ IDEA

      November 29, 2022

      Things To Do After Installing IntelliJ IDEA

      December 20, 2022

      How to Set Proxy for IntelliJ IDEA?

      December 20, 2022

      How to Set Proxy for Maven?

      December 6, 2022

      How to Create a Fat JAR Using Maven?

      December 5, 2022

      Things To Do After Installing IntelliJ IDEA

      December 20, 2022

      How to Set Proxy for IntelliJ IDEA?

      December 20, 2022

      Manage GitHub Artifact Storage Quota

      December 6, 2022

      Run NiFi Cluster in Docker with SSL Enabled

      December 6, 2022
    • Hello World
      1. Framework
      2. Library
      3. View All

      JPA Hello World! using Hibernate and MySQL

      December 3, 2022

      Jersey 3.x – Hello World!

      December 2, 2022

      Microservices Framework for Java (MSF4J) – Hello World!

      December 1, 2022

      How to Parse PCAP files in Java?

      November 30, 2022

      JPA Hello World! using Hibernate and MySQL

      December 3, 2022

      Jersey 3.x – Hello World!

      December 2, 2022

      Microservices Framework for Java (MSF4J) – Hello World!

      December 1, 2022

      How to Parse PCAP files in Java?

      November 30, 2022
    • More
      • Privacy Policy
        • Java Helps
        • Android Apps
      • Contact US
      • About
    Facebook X (Twitter) Instagram
    Java HelpsJava Helps
    Home » Jersey 3.x – Hello World!
    Hello World
    Hello World
    Framework

    Jersey 3.x – Hello World!

    Gobinath LoganathanBy Gobinath LoganathanDecember 2, 2022Updated:December 4, 2022No Comments2 Mins Read
    Share
    Facebook Twitter LinkedIn Pinterest Email

    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

    • Java Development Kit
    • IntelliJ Idea Community/Ultimate or Eclipse with Maven Support
    • Apache Maven (Not necessary if you are not going to build the project outside of the IDE)

    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.

    jersey-hello-world

    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.

    framework hello world java
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email

    Related Posts

    What is Garbage Collection?

    December 7, 2022

    JPA Hello World! using Hibernate and MySQL

    December 3, 2022

    How to Run Apache NiFi Docker on Mac M1?

    December 1, 2022

    Microservices Framework for Java (MSF4J) – Hello World!

    December 1, 2022

    How to Parse PCAP files in Java?

    November 30, 2022

    Create A New Maven Project In IntelliJ IDEA

    November 29, 2022
    Don't Miss
    Configure

    Things To Do After Installing IntelliJ IDEA

    December 20, 2022

    IntelliJ Idea: the famous IDE for JVM languages including Java, Scala, and Kotlin. If you…

    How to Set Proxy for IntelliJ IDEA?

    December 20, 2022

    What is Garbage Collection?

    December 7, 2022

    Manage GitHub Artifact Storage Quota

    December 6, 2022
    Our Picks

    Things To Do After Installing IntelliJ IDEA

    December 20, 2022

    How to Set Proxy for IntelliJ IDEA?

    December 20, 2022

    What is Garbage Collection?

    December 7, 2022

    Manage GitHub Artifact Storage Quota

    December 6, 2022
    About Us
    About Us

    Java Helps is the platform to share all about the Java ecosystem. All the sample code available on Java Helps articles are published under Apache 2.0 License. Readers are free to use them according to the Apache 2.0 License.

    "The world is my town; its people my kinsmen."
    -Kaṉiyan Pūngunṟanār

    Email Us: www.javahelps@gmail.com

    Our Picks

    Things To Do After Installing IntelliJ IDEA

    December 20, 2022

    How to Set Proxy for IntelliJ IDEA?

    December 20, 2022

    What is Garbage Collection?

    December 7, 2022
    New Comments

      Type above and press Enter to search. Press Esc to cancel.

      Go to mobile version