In this tutorial we will create a simple RESTful web service with Jersey and Maven. The example code is available on Github.

1. Requirements

  • Eclipse EE 4.4.2 (important, make sure to use the EE version, otherwise you cannot adapt the Project Facets later on)
  • Apache Maven 3.3.3
  • M2E – useful Maven plugin for Eclipse
  • Tomcat 8
  • JDK 8

You can use other versions for Tomcat and the JDK, just make sure you adapt everything accordingly. The safest way is to use the same versions though.

2. Installation

Assuming JDK, Eclipse, Maven and Tomcat are installed correctly.

If you need help with installing either Maven or Tomcat, check out these tutorials.

  • Install Maven
  • Install Apache Tomcat

If the preconditions are fullfiled, we continue with installing M2E. This Eclipse plugin helps us with creating and building Maven projects automatically.

  1. In Eclipse go to Help ⇒ Install new Software ⇒ Add and use the location: http://download.eclipse.org/technology/m2e/releases
    Eclipse Software Repository
    Enter some name and the location of the package
  2.  Press ok and click through the following Forms with Next.
    Eclipse Software Repository
    Select the required Packages
  3. After download and restarting Eclipse, M2E is successfully installed.

3. Create Maven Project

  1. In the Eclipse Project Explorer (on the left hand side) right click and select new project. Search for Maven and select Maven Project. Click Next.
    Create Maven Project
    Search for "maven" and select Maven Project
  2. Keep the properties in the next window and click Next.
    Create Maven Project Step 2
    Keep the predefined properties and click Next
  3. In the following window select Maven archetype maven-archetype-webapp and click Next.
    Create Maven Project Webapp
    Select Maven arcehtype maven-archetype-webapp
  4. Enter a group Id and artifact Id and click Next. Eclipse will generate the project structure.
    New Maven Webapp HelloWorld
    Enter group and artifact Id
  5. You will see the following project structure in the Project Explorer:
    Maven Eclipse Project Explorer
    Project Explorer after creating Maven project
  6. You probably see javax.servlet.http.HttpServlet" was not found on the Java Build Path.
  7. You have to add the Apache Tomcat servlet to the Java Build Path. Please follow this tutorial on how to integrate the Apache Tomcat dependencies into your project.

4. Adapt the pom.xml

  1. Open the pom.xml in your root folder and replace the content:
    <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.tutorialacademy.rest</groupId> 	<artifactId>helloworld</artifactId> 	<packaging>war</packaging> 	<version>0.0.1-SNAPSHOT</version> 	<name>helloworld Maven Webapp</name> 	<url>http://maven.apache.org</url>  	<properties> 		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 	</properties>  	<repositories> 		<repository> 			<id>maven2-repository.java.net</id> 			<name>Java.net Repository for Maven</name> 			<url>http://download.java.net/maven/2/</url> 			<layout>default</layout> 		</repository> 	</repositories>  	<dependencies> 		<dependency> 			<groupId>junit</groupId> 			<artifactId>junit</artifactId> 			<version>3.8.1</version> 			<scope>test</scope> 		</dependency> 		<dependency> 			<groupId>com.sun.jersey</groupId> 			<artifactId>jersey-server</artifactId> 			<version>1.9</version> 		</dependency> 	</dependencies>  	<build> 		<sourceDirectory>src/main/java</sourceDirectory>  		<plugins>  			<plugin> 				<artifactId>maven-war-plugin</artifactId> 				<version>2.4</version> 				<configuration> 					<warSourceDirectory>WebContent</warSourceDirectory> 					<failOnMissingWebXml>false</failOnMissingWebXml> 				</configuration> 			</plugin>  			<plugin> 				<artifactId>maven-compiler-plugin</artifactId> 				<version>3.1</version> 				<configuration> 					<source>1.8</source> 					<target>1.8</target> 				</configuration> 			</plugin>  		</plugins>  	</build> 	 </project>                  
  2. Now some explanation for the pom.xml
    • <dependencies>: Libraries or jar files that are required. After a Maven project update (which we will perform later), Maven will download these libraries and add them to the Java Build Path.
    • <sourceDirectory>:Directory where Maven will find our packages and Java classes.
    • <plugins>:Addiontal information for Maven, e.g. compiler version

5. Adapt the web.xml

  1. Open the web.xml file located in your WEB-INF folder (srcmainwebappWEB-INF). You have to specify the servlet container, define start pages like index.php and locate the web service packages in your project.
  2. Replace the the web.xml with the following content:
    <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 	xmlns="http://xmlns.jcp.org/xml/ns/javaee" 	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 	id="WebApp_ID" version="3.1">  	<display-name>helloworld</display-name>  	<welcome-file-list> 		<welcome-file>index.html</welcome-file> 		<welcome-file>index.htm</welcome-file> 		<welcome-file>index.jsp</welcome-file> 		<welcome-file>default.html</welcome-file> 		<welcome-file>default.htm</welcome-file> 		<welcome-file>default.jsp</welcome-file> 	</welcome-file-list>  	<servlet> 		<servlet-name>helloworld</servlet-name> 		<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> 		<init-param> 			<param-name>com.sun.jersey.config.property.packages</param-name> 			<param-value>com.tutorialacademy.rest</param-value> 		</init-param> 		<load-on-startup>1</load-on-startup> 	</servlet>  	<servlet-mapping> 		<servlet-name>helloworld</servlet-name> 		<url-pattern>/rest/*</url-pattern> 	</servlet-mapping>  </web-app>
  3. The welcome-file-listspecifies which files are called per default, if available. The servlet and servlet-mapping specify the servlet container and the path to the servlet container. You can adapt display-name and servlet-name according to your requirements.
  4. Important:If you use different project or package etc., make sure the paths are adapted correctly. Especially<param-value>com.tutorialacademy.rest</param-value>.

6. Create a web service class

  1. Right click src/main and create a new folder called java
  2. Right click the new src/main/java source folder (belowJava Resources), click on NewPackage. We call it com.tutorialacademy.rest.
  3. Right click the new package, click on NewClass. We call it RESTfulHelloWorld.
  4. Copy the following code into the new class
    package com.tutorialacademy.rest;  import java.util.Date;  import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.Response;  @Path("/") public class RESTfulHelloWorld  { 	@GET 	@Produces("text/html") 	public Response getStartingPage() 	{ 		String output = "<h1>Hello World!<h1>" + 				"<p>RESTful Service is running ... <br>Ping @ " + new Date().toString() + "</p<br>"; 		return Response.status(200).entity(output).build(); 	} }
  5. Ignore the code and import errors for now, we have to adapt the Maven project to retrieve missing dependencies.

7. Adapt the Maven project

  1. You should adapt the JDK and Dynamic Web Module Version in the Project Facets. Therefore, right click on your project and select Properties. Select Project Facets.
    Adapt Project Facets
    Adapt the Dynamic Web Module to version 3.1
  2. We adapted the  JDK to 1.8. You need at least JDK 1.7 to switch to the latest Dynamic Web Module version 3.1. If you are unfamiliar with this topic, please use the same versions to avoid complicating errors.
  3. You may not be able to adapt the Dynamic Web Module version. Deselect it, adapt the JDK version to 1.8 or 1.7 and hit apply. Select the desired Dynamic Web Module version and click apply again.
  4. You can select the JAX RS 1.1 as well. Anyways, this will adapt automatically after the update of the project in the next chapter.
  5. Depending on your selected version, Errors and Warnings may differ from this tutorial.

8. Update the Maven project

  1. Right click the project and select MavenUpdate Project.You can press ALT + F5 as shortcut.
    Update Maven Project
    Select your project and click OK
  2. Now Maven will download missing dependencies, adapt the project structure according to the pom.xml.
  3. Important: Copy your web.xml into WebContent/WEB-INFfolder. You can delete the index.js.
  4. Your project structure should now look similar to this:
    Project Explorer Dynamic Web Module 3.1
    Compare if you have a similar project structure

9. Run Maven Build

  1. Right click your project and select Run As:
    Run as Maven Configuration
    Select run as Maven Build (6)
  2. Add "clean install" to the goals and click Apply and then Run:
    Maven Clean Install Goals
    Clean the project and rebuild it
  3. Now Maven generated a war file in the target folder:
    aven Clean Install Target
    Check if you have generated a war file and or error messages in the console
  4. If you do not see any errors but"[INFO] BUILD SUCCESS", we can start an run our project on the Tomcat server.

10. Run the RESTful project on Server

You can use the internal Eclipse web browser and Tomcat runtime or deploy it yourself.

  1. Deployment in Eclipse:
    1. Right click your project and select Run AsRun on Server
    2. Eclipse will open the internal web browser, but we have to adapt the URL to "http://localhost:8080/helloworld/rest/"
      RESTful Webservice Running
      You should see the Hello World message in your browser
  2. Deployment in Tomcat directly:
    1. Copy the "helloworld-0.0.1-SNAPSHOT.war" file into your %CATALINA_HOME%/webapps folder.
    2. Go to the command line (Winkey + R) and enter: %CATALINA_HOME%\bin\startup.bat
    3. Enter "http://localhost:8080/helloworld-0.0.1-SNAPSHOT/rest/"in your web browser:
      RESTful Webservice Running Browser
      Check if you see the Hello World message
  3. Now you created a RESTful web service using Maven and Tomcat.

If you have errors, exceptions or other problems feel free to comment and ask.