Spring Microservices RESTFul API Documentation With Swagger Part 1

DZone 's Guide to

Spring Microservices RESTFul API Documentation With Swagger Part 1

This article explains, how Spring boot Micro-services can be documented using Swagger2

· Microservices Zone ·
Free Resource

When back-end APIs are developed and exposed to the client, having good documentation is key. API documentation should be properly structured so that it provides all information related to the API. Without having proper documentation, the client using the API will struggle to access it. If this happens, the API is useless. There are several tools to document the APIs/RESTful Services. However, when you have developed a RESTful service using Spring Boot, then Swagger2 is the obvious choice for documentation because it makes your documentation very easy. 

  1. Swagger is the oldest and most mature spec. 
  2. Supports most languages.
  3. Easy to get started with.
  4. Good community support. 
  5. Has a Maven plugin and is easily configurable with a Spring Boot application.

In this and subsequent articles, we will see how can Swagger2 be configured with a Spring Boot RESTful service. In Part 1, I will show you how to configure your application without security and in Part 2, we'll add security. 

Configuration Without Security

The following steps need to be performed to include Swagger in your Spring Boot application and document the API. I will do it for a fresh project 

Step 1 — Create the Project Structure and create artifacts in the correct location.

Creating initial project structure

Step 2 — Add the following Swagger dependencies in your pom.xml file.

Adding dependencies in pom.xmlComplete pom.xml


Step 3 — Configuring Swagger in the application. To configure Swagger in the application, we need to create a config file, which will have all Swagger related configurations. I will explain each after this code snippet.


In above code snippet, you can see following configuration needs to be done:

  • @EnableSwagger2 - Annotation enables Swagger in the Support in the Class
  • select() - method invocation on Docket bean instance that returns an APISelectBuilder object, which provides the api() method and paths methods that need to be configured. 
  • api() - method filters the Controllers and methods that need to be documented. You can see I am setting the RestController Class as Filter. So, all classes that are annotated with RestController will be filtered.
  • Path() - method acts as an additional filter to generate documentation only for the path starting with  some specific String. In my case, we are allowing all options.

Now, you can see there are few customizations that I had done like:

  • Providing Produce and consume types. Here, I had configured two, "application/json" and "application/xml".
  • ApiInfo - We can set API Details for providing more details to the consumer, like setting name, emailId, terms and conditions, license etc. 
  • SecuritySchema - Will see in the next article (Swagger with Security)
  • SecurityContext - Will see in the next article (Swagger with Security)
  • globalResponseMessage - We can set global Response messages as well for handling errors like 500 and 404, and that will be applicable for all APIs that are being documented

There are a few more customization that can be performed on top of the Docket object.

Step 4 - Once Swagger Configurations are done, Controllers and Model objects need to be documented using available annotations. 

1. Annotations of End Points 

Any Class having the @RestController annotation will be considered as REST API Controllers (Resource).


In this Controller/Resource, you can see many annotations. Let's go one by one and see what they are responsible for:

  • @RestController - It is a combination of @Controller and @ResponseBody, and it suggests that this class will be considered as a REST API Controller that will accept all API call requests.
  • @RequestMapping - Class-level annotation that maps requests to items to controller. The method-level @RequestMapping annotations map web requests to the handler methods of the controller. 
  • @GetMapping, @PostMapping, @PatchMapping, @PutMapping, @DeleteMapping - To handle different requests coming from the client. 
  • @API - Class-level annotation to describe the API, and you can use this tag to explain the functionality of API.

RestController example

  • @ApiOperation - Method-level annotation that describes the end point and its response type. You can also override the default response messages of HTTP methods. You can use the @ApiResponse annotation to document other responses, in addition to the regular HTTP 200 OK, like this. 

GetMapping and API annotations

Service Class Implementation



Spring Boot application 


2. Annotations for Models 

@ApiModel and @ApiModelProperty can be used to document the Models used in the application. 

  • @ApiModel - Class-level annotation that helps to describe the Model. It has two main fields value and a description.
  • @ApiModelProperty- Field-level annotation that helps to describe the Field in detail. It has many properties; I am highlighting important ones you can use:
    • name - Unique Name
    • value - Detail of field
    • datatype - string, decimal, float, int etc
    • readOnly - true/false
    • example  - Sample Value

Step 5 - Once endpoint and Model documentation are done, start the Spring Boot application and access the following URL: http://localhost:8080/swagger-ui.html.

Once you access this url, Swager main page will get open which will have all your apis listed which you have documented 


In this page, you can see following things:

  1. Your Controller/Resource Customized name.
  2. All end points which was annotated by @ApiOperation and then you can see the description provided by you at right.
  3. API Info provided in Swagger config file.

When you access POST Method of Items for creating Item:

POST method of items for creating an Item

In this screenshot you can see following 

  1. Response Content Type - Set in Swagger Config. You can chose either XML or JSON. 
  2. Example Value - Picked from Model, documented by you. 
  3. Try Out - There will be a Try Out button. Once you click on it, a request will go to the server and perform the required operation:

Sending request to server for required operation

Swagger Documentation for Item Model 

Swagger documentation for Item model


Other than API Documentation, Swagger can also be used for defining your API Specs, and then later, you can perform API Validation as well. As it is integrated with your application, you can easily access the services.

I hope this is helpful. In the next article (Part 2), I will show you Swagger Configurations for adding security, so that only valid users can access Swagger documentation.

api documentation, microservices, spring boot, swagger 2, tutorial

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}