Swagger Documentation for Spring Boot rest API – Part II

In this post, we will show how to add swagger documentation to Spring boot rest API. We learned how to create a Spring Boot REST API. In Microservices’ world, these days documenting your API is a standard norm. Swagger provides a handy interface and a simple way to build these documentations that any client can test any moment. They don’t need to have all the services on their environment.

What is Swagger?

Swagger was intended to provide a standard, language-agnostic interface to REST APIs which allow anyone to understand the capabilities of a service without any source code, documentation of source code. You can find more details about Swagger here.

How to add swagger documentation?

In our previous post, we added Spring boot REST API. We will add swagger documentation to the same REST API.

  1. Add Maven dependencies

To start with, let’s add maven dependencies for swagger jars.

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
<scope>compile</scope>
</dependency>

2. Add Swagger bean in configuration

In our main starting Application class, we will add few configurations for setting up a bean which will handle swagger documentation. In below code, I show what I have added in Application.java file. Basically, we have created a new bean of type Docket which takes care of swagger configuration.

@EnableSwagger2
@SpringBootApplication(scanBasePackages = { "com.betterjavacode.benefits" })
public class Application {

public static void main(String[] args) {
 SpringApplication.run(Application.class, args);

}

@Bean
 public Docket benefitsApi() {
 return new Docket(DocumentationType.SWAGGER_2).groupName("Benefits")
 .apiInfo(apiInfo())
 .select()
 .apis(RequestHandlerSelectors.any())
 .paths(PathSelectors.any())
 .build()
 .pathMapping("/");

}

private ApiInfo apiInfo() {
 return new ApiInfoBuilder().title("Benefits REST Service")
 .description(" A simple REST service for Benefits software ")
 .contact(new Contact("Yogesh Mali", "https://betterjavacode.com", ""))
 .version("1.0")
 .build();
 }
}

3. Show Swagger documentation

Now once we have added the configuration, we can build our project with maven clean install. After successful build, run the project from eclipse as a Java application. We will access swagger documentation from URL http://localhost:8080/swagger-ui.html . This will look like below :

Swagger

Source code for this post is available at Spring-boot-rest-api-with-swagger.

Advertisements

4 thoughts on “Swagger Documentation for Spring Boot rest API – Part II

  1. Pingback: Error Handling and Logging in Spring Boot REST API – Part III | Code Junkie

  2. Pingback: Consuming a RESTful Webservice – Part IV | Code Junkie

  3. Pingback: Producing and Consuming SOAP Webservice with Spring Boot – Part V | Code Junkie

  4. Pingback: How To: AngularJS User Interface to CRUD Spring Boot REST API | Code Complete

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s