How To Add Swagger to Spring Boot Application

Oleksii Dushenin
3 min readMay 27, 2021

In a previous post, distributed Redis cache was added to Spring Boot Application. In this post, we will introduce API documentation in the form of Swagger. It allows you to review all possible API operations in your Spring Boot Application with a minimum effort. This documentation is helpful when you want to familiarize yourself with an existing API. Also, it is a useful tool when integrating with the front-end team. So, let’s add Swagger to our Spring Boot Application.

Basic Configuration

It is very easy to add Swagger out-of-the-box features to the Spring Boot Application. We should only add the next dependency:

implementation 'io.springfox:springfox-boot-starter:3.0.0'

You can check that everything is set up using the next URL:

http://localhost:8080/v2/api-docs

This URL contains JSON that describes an API.

However, Swagger UI is used much more frequently. It is available at the next URL:

http://localhost:8080/swagger-ui/

Please note, that this path is the correct one for the latest Swagger versions. For older versions it was usually swagger-ui.html.

In your browser you will be able to see available API.

E.g. create item API has the next description.

Swagger out-of-the-box features are more than enough to use it. Let’s see how can we make some customization.

Docklet

Let’s create a configuration.

@Configuration
public class SwaggerConfiguration {

@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo());
}

private ApiInfo apiInfo() {
return new ApiInfo(
"Spring Boot Simple REST API",
"Spring Boot Simple REST API Swagger Documentation",
"Version 1",
"urn:tos",
new Contact("Admin", "www.datamify.com", "info@datamify.com"),
"Apache 2.0",
"https://www.apache.org/licenses/LICENSE-2.0",
new ArrayList<>());
}

}

By doing this, we update some meta information. You can check out all possible options. Swagger UI will have the next form.

As you can see a lot of data were changed comparing to the initial screen. E.g. API name, version, website and contacts were changed.

Model Description

Swagger can use Hibernate Validation to describe model constraint.

For instance, let’s change CreateItemDto.

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class CreateItemDto {

@NotNull
@Size(min = 1, max = 255)
@ApiModelProperty(value = "Product item title")
private String title;

}

As you can see, we provided title description with @ApiModelProperty annotation. Also, @NotNull and @Size constraints were introduced. The model description will be changed based on these values.

Test an API

As you could notice, button Try it out is presented for each method. You can use it to execute requests to the server.

Summary

To sum up, in this post Swagger was introduced in Spring Boot Application.

The whole code can be found at Github v6.0.0-swagger branch.

Originally published at https://datamify.com on May 27, 2021.

--

--