Quarkus - Quarkus Extension for Spring Web API
This guide explains how a Quarkus application can leverage the well known Spring Web annotations to define RESTful services.
To complete this guide, you need:
less than 15 minutes
JDK 1.8+ installed with
JAVA_HOME
configured appropriatelyApache 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
, or download an archive.
The solution is located in the spring-web-quickstart
.
Creating the Maven project
This command generates a Maven project with a REST endpoint and imports the spring-web
extension.
The Quarkus maven plugin automatically generated a controller with the Spring Web annotations to define our REST endpoint (instead of the JAX-RS ones used by default)The file looks as follows:
GreetingControllerTest
Note that a test for the controller has been created as well:
Package and run the application
Run the application with: ./mvnw compile quarkus:dev
.Open your browser to .
The result should be: {"message": "hello"}
.
You can of course create a native image using the instructions of the Building a native executable guide.
Going further with an endpoint returning JSON
The GreetingController
above was an example of a very simple endpoint. In many cases however it is required to return JSON content.The following example illustrates how that could be achieved using a Spring RestController:
The corresponding test could look like:
It should be noted that when using the Spring Web support in Quarkus, Jackson is automatically added to the classpath and properly setup.
Supported Spring Web functionalities
The table below summarizes the supported annotations:
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
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.For this to function however, users need to add the quarkus-undertow
dependency.
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 will not have any effect. Moreover Spring infrastructureclasses (like for example) will not be executed.
More Spring guides
Quarkus support has more Spring compatibility features. See the following guides for more details: