Create Jersey JAX-RS Project with Maven

In this blog post I will share with you how to use Maven to generate a very simple Jersey application which you can then use to build RESTful web services for your mobile app. I will also show you how to edit pom.xml file to add needed dependencies, so that your Jersey app is able to convert JSON payload sent to your API Endpoint into a Java object and also be able to convert Java objects into JSON, so that it can be sent back to your mobile app as part of the Response.

Generate Sample Web App from Maven Archetype

To generate a very simple Java Web Application from one of the Maven archetypes we will use following command:

mvn archetype:generate
-DgroupId={project-package}
-DartifactId={project-name}
-DarchetypeArtifactId=maven-archetype-quickstart
-DinteractiveMode=false

where

{project-package} will need to be replaced with the desired package name for your project, and
{project-name} will need to be replaced with the name of your project

For example, I will use the below command to generate a new project with name AppAPI and package com.appsdeveloperblog.api

  1. Open terminal window on your computer and navigate to a folder where you want to create your new java project.
  2. Run the below command to generate new java project in selected folder:

mvn archetype:generate -DgroupId=com.appsdeveloperblog.api -DartifactId=AppAPI -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

This will create a new Maven Web App which you can open in your favourite Java Development Environment and edit.

Adding Jersey Dependency to pom.xml

There are different modules and dependencies you can add to your Jersey application depending on your specific need. A list of different modules to support Jersey you can find on Modules and dependencies page at https://jersey.java.net/documentation/latest/modules-and-dependencies.html

I usually add the primary jaxrs-ri artifact which is “all-in-one” and allows me to start using Jersey to build Restful WebServices for my mobile apps.

<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.bundles/jaxrs-ri -->
       <dependency>
           <groupId>org.glassfish.jersey.bundles</groupId>
           <artifactId>jaxrs-ri</artifactId>
           <version>2.25</version>
       </dependency>

Adding Jersey Media Moxy Dependency to pom.xml To Support JSON

Having added the above jaxrs-ri dependency will enable our Jersey app to convert XML payload into Java objects and then Java objects back to XML. But to make your Jersey JAX-RS application be able to convert incoming JSON payload into Java object and also convert Java objects back to JSON objects there is more dependency that you need to add. It is called: jersey-media-moxy

<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-moxy -->
<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-moxy</artifactId>
    <version>2.25</version>
</dependency>

After you add the above two dependencies your pom.xml file should look like this:

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.appsdeveloperblog</groupId>
    <artifactId>AppAPI</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>AppAPI Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.bundles/jaxrs-ri -->
        <dependency>
            <groupId>org.glassfish.jersey.bundles</groupId>
            <artifactId>jaxrs-ri</artifactId>
            <version>2.25</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-moxy -->
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
            <version>2.25</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>AppAPI</finalName>
    </build>
</project>

You are now ready to build your Jersey App!

Building Your Jersey App 

To build your Jersey and generate the WAR file which you can then deploy to your Servlet Container like Apache Tomcat, do the following:

  1. In your terminal window, navigate to your project folder
  2. While inside your project’s folder run the below command:
mvn install

This will generate a deployable .war file and will place it in a folder called target which you can also find inside of your project’s folder.

Leave a Reply

Your email address will not be published. Required fields are marked *