Spring Boot and Swagger
Swagger and Spring Boot
Spring Boot is Java’s premiere framework for the development of RESTful web services.
Swagger is the industry’s leading tool for testing, documenting and building RESTful APIs.
It’s just natural to want to integrate the two technologies together, and fortunately, doing so isn’t that hard, so long as you’re willing to update your Maven POM file or edit your Gradle build file.
How to add Swagger to Spring Boot
To add Swagger to any Spring Boot app, simply follow these steps:
- Ensure the Spring Web starter is included in your Spring Boot project
- Update your Spring Boot app with Swagger’s Maven GAV or Gradle coordinates
- Rebuild and redeploy your Swagger-enabled Spring Boot app
- Access the Spring Boot Swagger web app at: http://localhost:8080/swagger-ui
- Test your RESTful Spring Boot APIs with the OpenAPI compliant Swagger UI
The following code shows the coordinates for the SpringDoc OpenAPI Maven dependency for Swagger and Spring Boot:
Swagger, Spring Boot and Maven
Spring Boot developers are spoiled by the number of useful starters included by default in the Spring Initializr. However, Spring Boot Swagger support isn’t one of them.
To add Swagger support to Spring Boot, you’ll have to find the appropriate dependency from a Maven repository and add it to your application manually.
Always check online for the latest version, but the dependency to add to get Spring Boot and Swagger integration at the time of writing is as follows:
Swagger, Spring Boot and Gradle
For those who use Gradle, your application’s build file can be updated with the following coordinates to integrate Swagger and Spring Boot:
implementation group: 'org.springdoc', name: 'springdoc-openapi-starter-webmvc-ui', version: '2.6.0'
What is the Swagger URL?
With Spring Boot and Gradle successfully integrated, the Swagger web app can be accessed from the following URL:
http://localhost:8080/swagger-ui/index.html
Benefits of Swagger Spring integration
With Swagger and Spring Boot integrated, applications can be tested manually through the web interface the OpenAPI tool provides. However, there’s more to Swagger than simplifying the manual testing process. Other key benefits of integrating Swagger and Spring Boot include:
- OpenAPI provides a Java-based, intuitive way to document RESTful APIs
- Swagger can act as an automatic code generator for REST based web services
- OpenAPI will validate APIs to ensure the documentation and implementation match
From testing to documentation, integrating you RESTful Spring Boot applications with Swagger will help accelerate development as it simplifies testing and encourages developers to properly document their applications.