While you are encouraged to use JAX-RS annotation for defining REST endpoints, Quarkus provides a compatibility layer for Spring Web in the form of the spring-web extension.

This guide explains how your Quarkus application can leverage the well known Spring Web annotations to define RESTful services.

Prerequisites

To complete this guide, you need:

  • less than 15 minutes

  • an IDE

  • JDK 1.8+ installed with JAVA_HOME configured appropriately

  • Apache Maven 3.5.3+

Solution

We recommend that you follow the instructions in the next sections and create the application step by step. However, you can go right to the completed example.

Clone the Git repository: git clone https://github.com/quarkusio/quarkus-quickstarts.git, or download an archive.

The solution is located in the using-spring-web directory.

Creating the Maven project

First, we need a new project. Create a new project with the following command:

mvn io.quarkus:quarkus-maven-plugin:0.21.1:create \
    -DprojectGroupId=org.acme \
    -DprojectArtifactId=using-spring-web \
    -DclassName="org.acme.spring.web.GreetingController" \
    -Dpath="/greeting" \
    -Dextensions="spring-web"

This command generates a Maven project with a REST endpoint and imports the spring-web extension.

Writing the GreetingController

Before writing the GreetingController, let’s first create a Greeting class that will be used as a response from the controller. Furthermore let’s create a Spring bean called GreetingBean that will be used by the controller.

package org.acme.spring.web;

public class Greeting {

    private final String message;

    public Greeting(String message) {
        this.message = message;
    }

    public String getMessage() {
        return message;
    }
}
package org.acme.spring.web;

import org.springframework.stereotype.Service;

@Service
public class GreetingBean {

    public String greet(String input) {
        return "HELLO " + input.toUpperCase() + "!";
    }
}

The Quarkus Maven plugin automatically generated a src/main/java/org/acme/spring/web/GreetingController.java file. However since we are going to be using Spring Web annotations to define our REST endpoint (instead of the JAX-RS ones used by default), we need to change the GreetingController to have the following content:

package org.acme.spring.web;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.PathVariable;

@RestController
@RequestMapping("/greeting")
public class GreetingController {

    private final GreetingBean greetingBean;

    public GreetingController(GreetingBean greetingBean) {
        this.greetingBean = greetingBean;
    }

    @GetMapping("/{name}")
    public Greeting hello(@PathVariable(name = "name") String name) {
        return new Greeting(greetingBean.greet(name));
    }
}

Update the test

We also need to update the functional test to reflect the changes made to the endpoint. Change its content to:

package org.acme.spring.web;

import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.Test;

import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.is;

@QuarkusTest
public class GreetingControllerTest {

    @Test
    public void testGreeting() {
        given()
            .when().get("/greeting/world")
            .then()
                .statusCode(200)
                .body("message", is("HELLO WORLD!"));
    }

}

Package and run the application

Run the application with: ./mvnw compile quarkus:dev. Open your browser to http://localhost:8080/greeting/world.

The result should be: {"message": "HELLO WORLD!"}.

Run the application as a native executable

You can of course create a native image using the instructions of the Building a native executable guide.

Supported Spring Web functionalities

Quarkus currently supports a subset of the functionalities that Spring Web provides. More specifically Quarkus supports the REST related features of Spring Web (think of @RestController instead of @Controller).

Annotations

The table below summarizes the supported annotations:

Table 1. Supported Spring Web annotation
Name Comments

@RestController

@RequestMapping

@GetMapping

@PostMapping

@PutMapping

@DeleteMapping

@PatchMapping

@RequestParam

@RequestHeader

@MatrixVariable

@PathVariable

@CookieValue

@RequestBody

@ResponseStatus

@ExceptionHandler

Can only be used in a @RestControllerAdvice class, not on a per-controller basis

@RestControllerAdvice

Only the @ExceptionHandler capability is supported

Controller method return types

The following method return types are supported:

  • Primitive types

  • String (which will be used as a literal, no Spring MVC view support is provided)

  • POJO classes which will be serialized via JSON

  • org.springframework.http.ResponseEntity

Controller method return types

In addition to the method parameters that can be annotated with the appropriate Spring Web annotations from the previous table, javax.servlet.http.HttpServletRequest and javax.servlet.http.HttpServletResponse are also supported.

Important Technical Note

Please note that the Spring support in Quarkus does not start a Spring Application Context nor are any Spring infrastructure classes run. Spring classes and annotations are only used for reading metadata and / or are used as user code method return types or parameter types. What that means for end users, is that adding arbitrary Spring libraries to the classpath will not be taken into account. Moreover Spring infrastructure things like org.springframework.beans.factory.config.BeanPostProcessor will not be executed.