Hello Jersey and Jetty

Hello Jersey and Jetty

Seven Easy steps to get Jetty and Jersey working together

When you need a servlet and don’t want to have to run your own web server like JBoss or Tomcat, Jetty is very handy. I like to use JAX-RS annotations and am very familiar with Jersey so I wanted to write a step by step tutorial for how to combine Jetty and Jersey. Instructions assume you know how to use Maven .

1. Use mvn archetype:generate to create your java project. Archetypes make project set up so much easier because the archetype will generate a base for you.

2. Add the following dependencies to your pom:

[code language=”xml”]<dependency>


Note: jetty.version: 9.2.10.v20150310, jersey.version: 1.19

This should work with any non glassfish version of the jersey. I was having trouble getting the glassfish version to scan packages.

3. In order to be able to run mvn jetty:run from the command line we need to also add the jetty build plugin to our pom.xml. Add this plugin to your section of your pom.xml.

[code language=”xml”]<build>


4. Make a simple Servlet class in the package of your choosing. Here’s an example of the ‘Hello’ Servlet:

[code language=”Java”]@Path("/hello")
public class App
public Response sayHello(@DefaultValue("world") @QueryParam("name") String name){
return Response.ok("Hello "+name).build();


The @Path annotation dictates the path to navigate from the ‘root’ of the web server to get to your servlet. The @GET annotation defines the method as a GET request. The @Produces annotation dictates the type of data that is returned when making this request.

5. Add a web.xml file in your src/main/webapp/WEB-INF folder:

[code language=”xml”]<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<display-name>Restful Web Application</display-name>



The ‘com.sun.jersey.spi.container.servlet.ServletContainer’ defines the servlet container to use. The important param in here is the ‘com.sun.jersey.config.property.packages’ Jersey will scan all classes found under the param value defined here. So it’s important for this param-value to match the package your Servlet code is contained in.

6. Run the command mvn jetty:run from the terminal(if on windows you can do this from the cmd prompt or Cygwin).

The end log from your prompt should look similar to this(minus the package names I used of course):


INFO: Scanning for root resource and provider classes in the packages:


May 17, 2015 6:04:40 AM com.sun.jersey.api.core.ScanningResourceConfig logClasses

INFO: Root resource classes found:

class mkw.jetty.App

May 17, 2015 6:04:40 AM com.sun.jersey.api.core.ScanningResourceConfig init

INFO: No provider classes found.

May 17, 2015 6:04:41 AM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate

INFO: Initiating Jersey application, version ‘Jersey: 1.19 02/11/2015 03:25 AM’

2015-05-17 06:04:41.854:INFO:oejsh.ContextHandler:main: Started o.e.j.m.p.JettyWebAppContext@29d334c{/,file:/C:/Users/Marcus/workspace/jetty-tutorial/src/main/webapp/,AVAILABLE}{file:/C:/Users/Marcus/workspace/jetty-tutorial/src/main/webapp/}

2015-05-17 06:04:41.855:WARN:oejsh.RequestLogHandler:main: !RequestLog

2015-05-17 06:04:41.984:INFO:oejs.ServerConnector:main: Started ServerConnector@4aedaf61{HTTP/1.1}{}

2015-05-17 06:04:41.985:INFO:oejs.Server:main: Started @6755ms

[INFO] Started Jetty Server


7. Now navigate to http://localhost:8080/hello?name=Test and your browser should be telling you hello.

Leave a Reply

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