Image of Building REST web service using Jersey


Table of Contents


Since Java 5, JAX-RS specification has been announced in order to support building REST web services according to the REST architectural pattern.

JAX-RS is a set of interfaces or APIs which provides the building blocks for building REST services in java. Although you can’t build a fully functional RESTful service using these interfaces, there exists a lot of popular frameworks which implement them and can be used on top of JAX-RS to build a RESTful service.

One of the most popular JAX-RS frameworks is Jersey:

Jersey RESTful Web Services framework is open source, production quality, framework for developing RESTful Web Services in Java that provides support for JAX-RS APIs and serves as a JAX-RS (JSR 311 & JSR 339) Reference Implementation

In this tutorial, we provide a step-by-step guide for building a Jersey REST web service with Maven.


  • Eclipse IDE (Mars release)
  • Java 1.8
  • Apache tomcat 8

1. Create maven web project

Open eclipse, then select File -> New -> Maven Project.

Keep the default selection in the first screen, then click “Next”.

In the next screen, search for “jersey-quickstart-web” archetype and select version 2.26 as the following:

jersey quickstart

In case you didn’t find the jersey archetype, then check this stackoverflow answer for how to add remote archetypes to eclipse.

Click “Next”.

In the final screen, fill the mandatory fields as the following:

jersey gate

  • “Group Id”: denotes a unique “dot” separated group name, which is used by external projects that link to yours, this field is normally set as the company name.
  • “Artifact Id”: denotes the name of the web project.
  • The main package of the project is the concatenation of “Group Id” + “Artifact Id”

Click “Finish”.

Here we go, the structure of the generated project looks like the following:

project structure

2. pom.xml

By default, the jersey archetype adds the required jersey dependencies under pom.xml, so you don’t have to worry about adding them manually.

            <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
            <!-- artifactId>jersey-container-servlet</artifactId -->
        <!-- uncomment this to get JSON support

In case you want to use another Jersey version, just change the value of “jersey.version” attribute under “properties” field:


3. web.xml

The following web.xml is generated by default:

<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
     see implementation details -->
<web-app version="2.5" xmlns="" 
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-name>Jersey Web Application</servlet-name>

Using “load-on-startup” attribute along with “org.glassfish.jersey.servlet.ServletContainer” servlet instructs the JVM to load all Jersey implementation classes on application startup. You can define the package of the implementation classes under “init-param” attribute:


In this tutorial, the JVM would try to load all jersey implementation classes defined under com.programmer.gate.JerseyRestService , you can always change this parameter as you want.

Another important attribute is the “servlet-mapping” which defines the URL of the requests to be processed by Jersey classes. In our example, all requests which are prefixed by /rest/* are processed by our service.


By default, eclipse generates a Jersey implementation class called

 * Root resource (exposed at "myresource" path)
public class MyResource {
     * Method handling HTTP GET requests. The returned object will be sent
     * to the client as "text/plain" media type.
     * @return String that will be returned as a text/plain response.
    public String getIt() {
        return "Got it!";
  • @Path: defines the relative path of the resource, clients access the resource through concatenating the value of the @Path annotation with the REST mapping URI, so this resource would process requests like: /rest/myresource
  • @GET: defines a method which processes GET requests.
  • @Produces(MediaType.TEXT_PLAIN): the meta type of the returned result, in this case it’s plain text.

5. Deploy the service

When deploying Jersey web service, always be aware of Jersey/JRE conflicts i.e. Jersey 2.26+ only works with JRE8+. If you try to run it with JRE7, the application wouldn’t start up and you’ll get “unsupported major.minor version” exception.

In our example, we deploy the web service on Tomcat 1.8/JRE8 (if you haven’t setup tomcat on eclipse, then follow this guide).

We then initiate a GET request to “MyResource” from the browser and this is the result:

result image

That’s all, hope you find it useful ?


Since Java 5, JAX-RS specification has been announced in order to support building REST web services according to the REST architectural pattern.

Next Steps

If you're interested in learning more about the basics of Java, coding, and software development, check out our Coding Essentials Guidebook for Developers, where we cover the essential languages, concepts, and tools that you'll need to become a professional developer.

Thanks and happy coding! We hope you enjoyed this article. If you have any questions or comments, feel free to reach out to

Final Notes