Quarkus - Using OpenAPI and Swagger UI

This guide explains how your Quarkus application can expose its API description through an OpenAPI specification andhow you can test it via a user-friendly UI named Swagger UI.

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+

Architecture

In this guide, we create a straightforward REST application to demonstrate how fast you can expose your APIspecification and benefit from a user interface to test it.

Solution

We recommend that you follow the instructions in the next sections and create the application step by step.However, you can skip 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 openapi-swaggerui-quickstart directory.

Creating the Maven project

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

  1. mvn io.quarkus:quarkus-maven-plugin:1.0.0.CR1:create \
  2. -DprojectGroupId=org.acme \
  3. -DprojectArtifactId=openapi-swaggerui-quickstart \
  4. -DclassName="org.acme.openapi.swaggerui.FruitResource" \
  5. -Dpath="/fruits" \
  6. -Dextensions="resteasy-jsonb"
  7. cd openapi-swaggerui-quickstart

This command generates the Maven project with a /fruits REST endpoint.

Expose a REST Resource

We will create a Fruit bean and a FruitResouce REST resource(feel free to take a look to the Writing JSON REST services guide if your want more details on how to build a REST API with Quarkus).

  1. package org.acme.openapi.swaggerui;
  2. public class Fruit {
  3. public String name;
  4. public String description;
  5. public Fruit() {
  6. }
  7. public Fruit(String name, String description) {
  8. this.name = name;
  9. this.description = description;
  10. }
  11. }
  1. package org.acme.openapi.swaggerui;
  2. import javax.ws.rs.*;
  3. import javax.ws.rs.core.MediaType;
  4. import java.util.Collections;
  5. import java.util.LinkedHashMap;
  6. import java.util.Set;
  7. @Path("/fruits")
  8. @Produces(MediaType.APPLICATION_JSON)
  9. @Consumes(MediaType.APPLICATION_JSON)
  10. public class FruitResource {
  11. private Set<Fruit> fruits = Collections.newSetFromMap(Collections.synchronizedMap(new LinkedHashMap<>()));
  12. public FruitResource() {
  13. fruits.add(new Fruit("Apple", "Winter fruit"));
  14. fruits.add(new Fruit("Pineapple", "Tropical fruit"));
  15. }
  16. @GET
  17. public Set<Fruit> list() {
  18. return fruits;
  19. }
  20. @POST
  21. public Set<Fruit> add(Fruit fruit) {
  22. fruits.add(fruit);
  23. return fruits;
  24. }
  25. @DELETE
  26. public Set<Fruit> delete(Fruit fruit) {
  27. fruits.removeIf(existingFruit -> existingFruit.name.contentEquals(fruit.name));
  28. return fruits;
  29. }
  30. }

As we changed the API, we also need to update the test:

  1. package org.acme.openapi.swaggerui;
  2. import io.quarkus.test.junit.QuarkusTest;
  3. import org.junit.jupiter.api.Test;
  4. import javax.ws.rs.core.MediaType;
  5. import static io.restassured.RestAssured.given;
  6. import static org.hamcrest.CoreMatchers.is;
  7. import static org.hamcrest.Matchers.containsInAnyOrder;
  8. @QuarkusTest
  9. public class FruitResourceTest {
  10. @Test
  11. public void testList() {
  12. given()
  13. .when().get("/fruits")
  14. .then()
  15. .statusCode(200)
  16. .body("$.size()", is(2),
  17. "name", containsInAnyOrder("Apple", "Pineapple"),
  18. "description", containsInAnyOrder("Winter fruit", "Tropical fruit"));
  19. }
  20. @Test
  21. public void testAdd() {
  22. given()
  23. .body("{\"name\": \"Pear\", \"description\": \"Winter fruit\"}")
  24. .header("Content-Type", MediaType.APPLICATION_JSON)
  25. .when()
  26. .post("/fruits")
  27. .then()
  28. .statusCode(200)
  29. .body("$.size()", is(3),
  30. "name", containsInAnyOrder("Apple", "Pineapple", "Pear"),
  31. "description", containsInAnyOrder("Winter fruit", "Tropical fruit", "Winter fruit"));
  32. given()
  33. .body("{\"name\": \"Pear\", \"description\": \"Winter fruit\"}")
  34. .header("Content-Type", MediaType.APPLICATION_JSON)
  35. .when()
  36. .delete("/fruits")
  37. .then()
  38. .statusCode(200)
  39. .body("$.size()", is(2),
  40. "name", containsInAnyOrder("Apple", "Pineapple"),
  41. "description", containsInAnyOrder("Winter fruit", "Tropical fruit"));
  42. }
  43. }

Expose OpenAPI Specifications

Quarkus proposes a smallrye-openapi extension compliant with the Eclipse MicroProfile OpenAPIspecification in order to generate your API OpenAPI v3 specification.

You just need to add the openapi extension to your Quarkus application:

  1. ./mvnw quarkus:add-extension -Dextensions="openapi"

Now, we are ready to run our application:

  1. ./mvnw compile quarkus:dev

Once your application is started, you can make a request to the default /openapi endpoint:

  1. $ curl http://localhost:8080/openapi
  2. openapi: 3.0.1
  3. info:
  4. title: Generated API
  5. version: "1.0"
  6. paths:
  7. /fruits:
  8. get:
  9. responses:
  10. 200:
  11. description: OK
  12. content:
  13. application/json: {}
  14. post:
  15. requestBody:
  16. content:
  17. application/json:
  18. schema:
  19. $ref: '#/components/schemas/Fruit'
  20. responses:
  21. 200:
  22. description: OK
  23. content:
  24. application/json: {}
  25. delete:
  26. requestBody:
  27. content:
  28. application/json:
  29. schema:
  30. $ref: '#/components/schemas/Fruit'
  31. responses:
  32. 200:
  33. description: OK
  34. content:
  35. application/json: {}
  36. components:
  37. schemas:
  38. Fruit:
  39. properties:
  40. description:
  41. type: string
  42. name:
  43. type: string
If you do not like the default endpoint location /openapi, you can change it by adding the following configuration to your application.properties:
  1. quarkus.smallrye-openapi.path=/swagger

Hit CTRL+C to stop the application.

Loading OpenAPI Schema From Static Files

Instead of dynamically creating OpenAPI schemas from annotation scanning, Quarkus also supports serving static OpenAPI documents.The static file to serve must be a valid document conforming to the OpenAPI specification.An OpenAPI document that conforms to the OpenAPI Specification is itself a valid JSON object, that can be represented in yaml or json formats.

To see this in action, we’ll put OpenAPI documentation under META-INF/openapi.yaml for our /fruits endpoints.Quarkus also supports alternative OpenAPI document paths if you prefer.

  1. openapi: 3.0.1
  2. info:
  3. title: Static OpenAPI document of fruits resource
  4. description: Fruit resources Open API documentation
  5. version: "1.0"
  6. servers:
  7. - url: http://localhost:8080/openapi
  8. description: Optional dev mode server description
  9. paths:
  10. /fruits:
  11. get:
  12. responses:
  13. 200:
  14. description: OK - fruits list
  15. content:
  16. application/json: {}
  17. post:
  18. requestBody:
  19. content:
  20. application/json:
  21. schema:
  22. $ref: '#/components/schemas/Fruit'
  23. responses:
  24. 200:
  25. description: new fruit resource created
  26. content:
  27. application/json: {}
  28. delete:
  29. requestBody:
  30. content:
  31. application/json:
  32. schema:
  33. $ref: '#/components/schemas/Fruit'
  34. responses:
  35. 200:
  36. description: OK - fruit resource deleted
  37. content:
  38. application/json: {}
  39. components:
  40. schemas:
  41. Fruit:
  42. properties:
  43. description:
  44. type: string
  45. name:
  46. type: string

By default, a request to /openapi will serve the combined OpenAPI document from the static file and the model generated from application endpoints code.We can however change this to only serve the static OpenAPI document by adding mp.openapi.scan.disable=true configuration into application.properties.

Now, a request to /openapi endpoint will serve the static OpenAPI document instead of the generated one.

About OpenAPI document pathsQuarkus supports various paths to store your OpenAPI document under. We recommend you place it under META-INF/openapi.yml.Alternative paths are:-META-INF/openapi.yaml-META-INF/openapi.yml-META-INF/openapi.json-WEB-INF/classes/META-INF/openapi.yml-WEB-INF/classes/META-INF/openapi.yaml-WEB-INF/classes/META-INF/openapi.jsonLive reload of static OpenAPI document is supported during development. A modification to your OpenAPI document will be picked up on fly by Quarkus.

Use Swagger UI for development

When building APIs, developers want to test them quickly. Swagger UI is a great toolpermitting to visualize and interact with your APIs.The UI is automatically generated from your OpenAPI specification.

The Quarkus smallrye-openapi extension comes with a swagger-ui extension embedding a properly configured Swagger UI page.

By default, Swagger UI is only available when Quarkus is started in dev or test mode.If you want to make it available in production too, you can include the following configuration in your application.properties:
  1. quarkus.swagger-ui.always-include=true

By default, Swagger UI is accessible at /swagger-ui.

You can update this path by setting the quarkus.swagger-ui.path property in your application.properties:

  1. quarkus.swagger-ui.path=/my-custom-path
The value / is not allowed as it blocks the application from serving anything else.

Now, we are ready to run our application:

  1. ./mvnw compile quarkus:dev

You can check the Swagger UI path in your application’s log:

  1. 00:00:00,000 INFO [io.qua.swa.run.SwaggerUiServletExtension] Swagger UI available at /swagger-ui

Once your application is started, you can go to http://localhost:8080/swagger-ui and play with your API.

You can visualize your API’s operations and schemas.Visualize your API

You can also interact with your API in order to quickly test it.Interact with your API

Hit CTRL+C to stop the application.

Configuration Reference

OpenAPI

Configuration property fixed at build time - ️ Configuration property overridable at runtime

Configuration propertyTypeDefault
quarkus.smallrye-openapi.pathThe path at which to register the OpenAPI Servlet.string/openapi

Swagger UI

Configuration property fixed at build time - ️ Configuration property overridable at runtime

Configuration propertyTypeDefault
quarkus.swagger-ui.pathThe path of the swagger-ui servlet. The value / is not allowed as it blocks the application from serving anything else.string/swagger-ui
quarkus.swagger-ui.always-includeIf this should be included every time. By default this is only included when the application is running in dev mode.booleanfalse