How to configure Swagger to generate Restful API Doc for your Spring Boot Web Application

DZone 's Guide to

How to configure Swagger to generate Restful API Doc for your Spring Boot Web Application

Learn How to Enable Swagger in your Spring Boot Web Application

· Integration Zone ·
Free Resource

“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.”
– Martin Fowler

What is Swagger?

Swagger is a specification and complete framework implementation for describing, producing, consuming, and visualizing RESTful web services. The goal of Swagger is to enable client and documentation systems to update at the same pace as the server. The documentation of methods, parameters, and models are tightly integrated into the server code, allowing APIs to always stay in sync.

Why is Swagger useful?

The Swagger framework simultaneously solves server, client, and documentation/sandbox needs.

With Swagger’s declarative resource specification, clients can understand and consume services without knowledge of server implementation or access to the server code. The Swagger UI framework allows both developers and non-developers to interact with the API in a sandbox UI that gives clear insight into how the API responds to parameters and options.

Swagger happily speaks both JSON and XML, with additional formats in the works.

How to Enable Swagger in your Spring Boot Web Application ?

Step 1 : Include Swagger Spring MVC dependency in Maven


Step 2 : Create Swagger Java Configuration

  • Use the @EnableSwagger annotation.
  • Autowire SpringSwaggerConfig.
  • Define one or more SwaggerSpringMvcPlugin instances using springs @Bean annotation.
public class SwaggerConfig {
private SpringSwaggerConfig springSwaggerConfig;
public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig) {
this.springSwaggerConfig = springSwaggerConfig;
// Don't forget the @Bean annotation
public SwaggerSpringMvcPlugin customImplementation() {
return new SwaggerSpringMvcPlugin(this.springSwaggerConfig).apiInfo(
private ApiInfo apiInfo() {
ApiInfo apiInfo = new ApiInfo("SaurzCode API", "API for Saurzcode",
"Saurzcode API terms of service", "mail2saurzcode@gmail.com",
"Saurzcode API Licence Type", "Saurzcode API License URL");
return apiInfo;

Step 3 : Create Swagger UI using WebJar


Verify the API Configuration at  – http://localhost:8080/api-docs

You can see the Swagger API Docs at  http://localhost:8080/index.html

Swagger API Doc

Complete project is available at GitHub.


References :

Originally Shared at : 


api, enterprise-integration, rest, spring, swagger

Published at DZone with permission of Saurabh Chhajed , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}