Using the Quarkus extension for the Spring Web API
Abstract
Preface Copy linkLink copied to clipboard!
As an application developer, you can use Spring Web annotations to define RESTful services in your Quarkus application.
Providing feedback on Red Hat documentation Copy linkLink copied to clipboard!
We appreciate your feedback on our technical content and encourage you to tell us what you think. If you’d like to add comments, provide insights, correct a typo, or even ask a question, you can do so directly in the documentation.
You must have a Red Hat account and be logged in to the customer portal.
To submit documentation feedback from the customer portal, do the following:
- Select the Multi-page HTML format.
- Click the Feedback button at the top-right of the document.
- Highlight the section of text where you want to provide feedback.
- Click the Add Feedback dialog next to your highlighted text.
- Enter your feedback in the text box on the right of the page and then click Submit.
We automatically create a tracking issue each time you submit feedback. Open the link that is displayed after you click Submit and start watching the issue or add more comments.
Thank you for the valuable feedback.
Making open source more inclusive Copy linkLink copied to clipboard!
Red Hat is committed to replacing problematic language in our code, documentation, and web properties. We are beginning with these four terms: master, slave, blacklist, and whitelist. Because of the enormity of this endeavor, these changes will be implemented gradually over several upcoming releases. For more details, see our CTO Chris Wright’s message.
Quarkus provides a compatibility layer for using annotations from Spring Web to define REST endpoints for your application. This functionality is provided by the quarkus-spring-web
extension as an alternative to using the default JAX-RS annotations to define your REST endpoints.
The Spring compatibility layer in Quarkus does not start a Spring Application Context or execute any infrastructure classes that are provided by Spring (for example, org.springframework.beans.factory.config.BeanPostProcessor
) when you start your application. Quarkus can only read metadata from Spring classes and annotations and parse user code method return types and parameter types that are specific to Spring. However, when you add arbitrary libraries that are part of Spring Framework to your Quarkus application, such libraries will not function properly, because Quarkus is not designed to use them.
You can follow this guide and create the example that uses the Quarkus extension for the Spring Web API, or you can download and view the completed example. To view the completed Quarkus Spring Web example, download it as an archive or clone the Quarkus examples Git repository. You can find the Spring Web example in the spring-web-quickstart
directory.
Chapter 1. Prerequisites Copy linkLink copied to clipboard!
-
Have OpenJDK 11 installed and the
JAVA_HOME
environment variable set to match the path to the directory in which OpenJDK is installed on your system. - Have Apache Maven 3.6.2 or higher installed.
Chapter 2. Creating the Spring Web example Maven project Copy linkLink copied to clipboard!
You can create a new Quarkus project, automatically generate the REST controller class, and add the quarkus-spring-web
dependency with a single command using the Quarkus Maven plugin. You can also update the pom.xml
file and create the REST controller class and the REST controller test class manually.
Procedure
Use one of the following approaches that are shown shown in this section to create your Quarkus Spring Web example Maven project:
If you do not have a Maven project, you can create a new Maven project using the Quarkus Maven plugin. Enter the following command to:
- Create the Maven project directory structure
-
Create the
org.acme.spring.web.GreetingController
class that defines a REST endpoint for your application Import the
quarkus-spring-web
extensionYou must replace
<project_name>
with the name of the directory that contains your project files.Copy to Clipboard Copied! Toggle word wrap Toggle overflow
-
If you already have a Quarkus Maven project, you must add the
quarkus-spring-web
extension to it using the command line:
Navigate to the root directory of your project:
cd <project_name>
cd <project_name>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Add the
quarkus-spring-web
extension to thepom.xml
file of your project:./mvnw quarkus:add-extension -Dextensions="spring-web"
./mvnw quarkus:add-extension -Dextensions="spring-web"
Copy to Clipboard Copied! Toggle word wrap Toggle overflow With this command you add the following entry to your
pom.xml
file:pom.xml
<dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-spring-web</artifactId> </dependency>
<dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-spring-web</artifactId> </dependency>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Chapter 3. Creating the main class and test class for GreetingController Copy linkLink copied to clipboard!
When you create your project on the command line, the Quarkus Maven plugin automatically generates the GreetingController
class file with Spring Web annotations that defines the REST endpoint and a class file that contains the unit test for GreetingController
.
Procedure
Create the
src/main/java/org/acme/spring/web/GreetingController.java
file that contains the following code.src/main/java/org/acme/spring/web/GreetingController.java
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create the
src/test/java/org/acme/spring/web/GreetingControllerTest.java
file that contains the following code.src/test/java/org/acme/spring/web/GreetingControllerTest.java
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Chapter 4. Compiling and starting your Spring Web example Copy linkLink copied to clipboard!
Compile and start your example application using the Quarkus Maven Plugin. You can also compile and run your application as a native executable.
Procedure
Navigate to the root directory of your project:
cd <project_name>
cd <project_name>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Run the application in development mode using the Quarkus Maven Plugin:
./mvnw compile quarkus:dev
./mvnw compile quarkus:dev
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Navigate to
http://localhost:8080/greeting
Your browser displays the following message:Hello Spring
Hello Spring
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Chapter 5. Configuring the GreetingController to return a JSON response Copy linkLink copied to clipboard!
The GreetingController
that is automatically generated when you set up your Spring Web example is a simple endpoint that returns a text string as a response. In more complex applications, you might need to configure your REST controller to return a response in JSON format. The following example illustrates how you can configure a Spring RestController
to return JSON content:
Procedure
Expand your
GreetingController
class as shown in the example. The expanded class returns a JSON-formatted response that contains a greeting and a name. Note, that you must import thePathVariable
annotation class from Spring Web to ensure that your configuration works correctly:src/main/java/org/acme/spring/web/GreetingController.java
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
When you make changes to your REST endpoint, you must also update the class file that contains the unit tests for your REST endpoint:
src/test/java/org/acme/spring/web/GreetingControllerTest.java
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Note, that when you use the Spring Web compatibility layer in Quarkus, the com.fasterxml:jackson.core dependency is automatically added to the classpath of your application and configured.
Chapter 6. Enabling OpenAPI and Swagger-UI support in your Spring Web example Copy linkLink copied to clipboard!
You can add support for generating OpenAPI schema documents of your REST endpoints with Swagger-UI to your application by adding the quarkus-smallrye-openapi
extension.
Procedure
Enter the following command to add the
quarkus-smallrye-openapi
extension as a dependency of your Spring Web example. Adding the extension is enough to generate a basic OpenAPI schema document from your REST Endpoints:./mvnw quarkus:add-extension -Dextensions="io.quarkus:quarkus-smallrye-openapi"
./mvnw quarkus:add-extension -Dextensions="io.quarkus:quarkus-smallrye-openapi"
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Entering the command adds the following dependency to your
pom.xml
:pom.xml
<dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-smallrye-openapi</artifactId> </dependency>
<dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-smallrye-openapi</artifactId> </dependency>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Enter the following command to obtain the schema document from the
/q/openapi
:curl http://localhost:8080/q/openapi
curl http://localhost:8080/q/openapi
Copy to Clipboard Copied! Toggle word wrap Toggle overflow You receive a response with the generated OpenAPI schema document in YAML format:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Chapter 7. Adding MicroProfile OpenAPI annotations to your REST controller code Copy linkLink copied to clipboard!
You can add MicroProfile OpenAPI annotations to your rest controller code to generate a more detailed OpenAPI schema for your rest endpoints.
Procedure
Add the
@OpenApiDefinition
annotation at the class level of yourGreetingController
. Include the data that is shown in the example in the annotation:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Annotate your endpoint definitions using the
@Tag
annotation. Give a name and a description for each endpoint:Copy to Clipboard Copied! Toggle word wrap Toggle overflow The data that you provided in the annotations appears in the generated OpenAPI schema:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Chapter 8. Overview of Spring Web annotations supported in Quarkus Copy linkLink copied to clipboard!
The Spring compatibility layer in Quarkus supports a limited subset of features that Spring Web provides. Specifically, Quarkus supports only the REST-related annotations from Spring Web, for example, @RestController
, but not @Controller
.
Quarkus supports the following annotations from Spring Web:
Chapter 9. Overview of Spring Web annotations and their JAX-RS equivalents Copy linkLink copied to clipboard!
The following table shows how Spring Web annotations can be converted to JAX-RS annotations.
Spring | JAX-RS | Notes |
---|---|---|
|
There is no equivalent in JAX-RS. Annotating a class with | |
|
| |
|
| |
|
| |
|
| |
|
| |
| No equivalent in JAX-RS. Method parameters corresponding to the body of the request are handled in JAX-RS without requiring any annotation. | |
| No equivalent in JAX-RS. | |
| No equivalent in JAX-RS. | |
|
No equivalent annotation in JAX-RS. Exceptions are handled by implementing |
Chapter 10. Controller method parameter types supported in Quarkus Copy linkLink copied to clipboard!
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.
Chapter 11. Controller method return types supported in Quarkus Copy linkLink copied to clipboard!
The following method return types are supported when using Spring Web on Quarkus:
- Primitive types
- String (Used as a literal. Quarkus does not support Spring MVC view)
- POJO classes that are serialized using JSON
-
org.springframework.http.ResponseEntity
Chapter 12. Exception handler method parameter types supported in Quarkus Copy linkLink copied to clipboard!
The Quarkus extension for Spring Web API supports the following exception handler method parameter types. (The order in which the types are listed is arbitrary and does not reflect the order of preference in which individual parameter types should be used):
-
An exception argument: declared as a general
Exception
or as a more specific exception. This also serves as a mapping hint if the annotation itself does not specify the exception types using itsvalue()
. -
Request or response objects (or both) (typically from the Servlet API). You can choose any specific request or response type, for example
ServletRequest
orHttpServletRequest
. You must add thequarkus-undertow
dependency to your project to use Servlet API.
Other parameter types mentioned in the Spring ExceptionHandler
Java API documentation are not supported in Quarkus
Chapter 13. Exception handler method return types supported in Quarkus Copy linkLink copied to clipboard!
The following method return types are supported when using Spring Web in Quarkus:
-
org.springframework.http.ResponseEntity
-
java.util.Map
Other return types mentioned in the Spring ExceptionHandler
Java API documentation are not supported in Quarkus.