Creating web application with Spring Boot and Jersey

Cassio Mazzochi Molin |

Spring Boot helps you accelerate the application development and makes it easy to create standalone applications that you can “just run”. Spring Boot embeds a web server, such as Tomcat or Jetty, in the application. So there’s no need to deploy a WAR file. It’s packed as a JAR and, to run it, you just need java -jar.

Jersey is the reference implementation of the JAX-RS 2.0 specification and provides a solid API for creating and consuming web services in Java.

In this post, let’s create a sample application with Spring Boot and Jersey.

Ensure your pom.xml file declares spring-boot-starter-parent as the parent project and declares all the dependencies and plugins shown below:

<project xmlns="" xmlns:xsi=""






The Spring Boot Maven plugin collects all the JARs on the classpath and builds a single, runnable uber JAR, which makes it more convenient to execute and transport the application. Besides it, the plugin searches for the public static void main() method to flag as a runnable class.

Create a Jersey resource class annotated with @Path and define a resource method to handle GET requests, producing text/plain:

public class GreetingResource {

    public Response getGreeting() {
        return Response.ok("Hello, World!").build();

Create a class that extends ResourceConfig or Application to register the Jersey resources:

public class JerseyConfig extends ResourceConfig {

    private void init() {

And finally create a Spring Boot class to execute the application:

public class Application {

    public static void main(String[] args) {, args);

To test this web service, we can use the JAX-RS Client API:

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

    private int port;

    private URI uri;

    public void setUp() throws Exception {
        this.uri = new URI("http://localhost:" + port);

    public void testGreeting() {

        Client client = ClientBuilder.newClient();
        Response response ="api").path("greetings")

        String entity = response.readEntity(String.class);
        assertEquals("Hello, World!", entity);

To compile and run the application, follow these steps:

  • Open a command line window or terminal.
  • Navigate to the root directory of the project, where the pom.xml resides.
  • Compile the project: mvn clean compile.
  • Package the application: mvn package.
  • Look in the target directory. You should see a file with the following or a similar name: spring-jersey-1.0-SNAPSHOT.jar.
  • Change into the target directory.
  • Execute the JAR: java -jar spring-jersey-1.0-SNAPSHOT.jar.
  • The application should be available at http://localhost:8080/api/greetings.

This example is available on GitHub.