Configure Jersey with Annotations only

In this tutorial we show you how to Configure Jersey with Annotations. By using only Annotations to configure jersey, the web.xml file becomes obsolete. This results in cleaner code which is always a good thing. We need a class that extends the which will register the jersey application with the help of the @ApplicationPath annotation.


Add jersey dependencies to your classpath.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=""

    <name>JERSEY - ${project.artifactId}</name>




Simple Rest Service Example

This resource is mapped to the path courses and produces a json list of courses.


import java.util.ArrayList;
import java.util.List;

public class CourseRestService {

    public List<Course> fetchAll() {
        List<Course> courses = new ArrayList<Course>();
        courses.add(new Course(1, "Configure Jersey with annotations"));
        courses.add(new Course(2, "Configure Jersey without web.xml"));
        return courses;


We use a simple POJO.


public class Course {

    private Integer id;
    private String name;

    public Course(Integer id, String name) { = id; = name;

    public Integer getId() {
        return id;

    public void setId(Integer id) { = id;

    public String getName() {
        return name;

    public void setName(String name) { = name;

Configure Jersey with Annotations

We need to extend the class to configure jersey. We can manually register the rest services by overriding the getClasses() method. But in this example we use a package scanner to automatically pickup rest services in our application. By overriding the getProperties() metod and registering the property jersey.config.server.provider.packages with the packages to search for rest services in our project, jersey is able to automatically register the rest services. Finally we need to map a context path that jersey must listen to. We can achieve this by annotating our class with the @ApplicationPath annotation.


import java.util.HashMap;
import java.util.Map;


public class ApplicationConfig extends Application {

    public Map<String, Object> getProperties() {
        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put("jersey.config.server.provider.packages", "");
        return properties;


URL: http://localhost:8080/jersey-no-webxml/api/courses

configure jersey with annotations



You may also like...