Spring Boot Jersey tutorial

Spring Boot Jersey tutorial shows how to set up a simple RESTFul application with Jersey in a Spring Boot application. Jersey is an alternative to Spring RESTFul applications created with @RestController.

Spring is a popular Java application framework for creating enterprise applications. Spring Boot is the next step in evolution of Spring framework. It helps create stand-alone, production-grade Spring based applications with minimal effort. It promotes using the convention over configuration principle over XML configurations.

RESTFul application

A RESTFul application follows the REST architectural style, which is used for designing networked applications. RESTful applications generate HTTP requests performing CRUD (Create/Read/Update/Delete) operations on resources. RESTFul applications typically return data in JSON or XML format.


Java API for RESTful Web Services (JAX-RS) is a Java programming language API specification that provides support in creating web services according to the Representational State Transfer (REST) architectural pattern. JAX-RS uses annotations to simplify the development and deployment of web service clients and endpoints. JAX-RS is an official part of Java EE.


Jersey is an open source framework for developing RESTful Web Services in Java. It is a reference implementation of the Java API for RESTful Web Services (JAX-RS) specification.

Spring Boot Jersey example

The following application is a simpe Spring Boot RESTful application created with Jersey.

$ tree
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── zetcode
    │   │           ├── Application.java
    │   │           ├── config
    │   │           │   └── JerseyConfig.java
    │   │           └── endpoint
    │   │               ├── HelloEndpoint.java
    │   │               └── ReverseReturnEndpoint.java
    │   └── resources
    └── test
        └── java
            └── com
                └── zetcode
                    └── ApplicationTests.java

This is the project structure.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 

This is the Maven build file. Spring Boot starters are a set of convenient dependency descriptors which greatly simplify Maven configuration. The spring-boot-starter-parent has some common configurations for a Spring Boot application. The spring-boot-starter-jersey is a starter for building RESTful web applications using JAX-RS and Jersey. It is an alternative to spring-boot-starter-web. The spring-boot-starter-test is a starter for testing Spring Boot applications with libraries including JUnit, Hamcrest and Mockito.

The spring-boot-maven-plugin provides Spring Boot support in Maven, allowing us to package executable JAR or WAR archives. Its spring-boot:run goal runs the Spring Boot application.

    port: 8086
    context-path: /api

        banner-mode: "off"     

            springframework: ERROR

In the application.yml file we write various configuration settings of a Spring Boot application. We set the port and the context path. With the banner-mode property we turn off the Spring banner.

We set the logging level for spring framework to ERROR. The application.yml file is located in the in the src/main/resources directory.

package com.zetcode.config;

import com.zetcode.endpoint.HelloService;
import com.zetcode.endpoint.ReverseService;
import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.context.annotation.Configuration;

public class JerseyConfig extends ResourceConfig {

    public JerseyConfig() {

JerseyConfig registers two service classes.

package com.zetcode.service;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import org.springframework.stereotype.Service;

public class HelloService {

    public String hello() {
        return "Hello from Spring";

This is the HelloService. The @Path annotation defines the URL to which the service class will respond. HelloService is annotated also with Spring's @Service for autodetection. Our service method simply returns "Hello from Spring" message.

package com.zetcode.service;

import javax.validation.constraints.NotNull;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import org.springframework.stereotype.Service;

public class ReverseService {

    public String reverse(@QueryParam("data") @NotNull String data) {
        return new StringBuilder(data).reverse().toString();

The reverse() service method returns a string which is reversed. It accepts one parameter, which cannot be null. @QueryParam binds the value(s) of a HTTP query parameter to a resource method parameter.

package com.zetcode;

import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class ApplicationTests {

    private TestRestTemplate restTemplate;

    public void hello() {
        ResponseEntity<String> entity = this.restTemplate.getForEntity("/hello",
        assertThat(entity.getBody()).isEqualTo("Hello from Spring");

    public void reverse() {
        ResponseEntity<String> entity = this.restTemplate
                .getForEntity("/reverse?data=regit", String.class);

    public void validation() {
        ResponseEntity<String> entity = this.restTemplate.getForEntity("/reverse",

In the ApplicationTests, we test the two endpoints.

package com.zetcode;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);

The Application sets up the Spring Boot application. The @SpringBootApplication enables auto-configuration and component scanning.

$ mvn spring-boot:run

With mvn spring-boot:run command, we run the application. The application is deployed on embedded Tomcat server.

$ curl localhost:8086/api/hello
Hello from Spring

With the curl command, we connect to the hello endpoint.

$ curl localhost:8086/api/reverse?data=summer

The summer's characters are reversed.

In this tutorial, we have created a simple RESTFul application in Spring Boot with Jersey, which is the reference implementation of the JAX-RS specification. You might also be interested in the related tutorials: