Spring Boot RESTful WebService API Documentation using Swagger 2

Swagger 2 is an open source project used to describe and document RESTful APIs.

Springfox is popular Swagger 2 implementation used for Spring Boot applications. It builds interactive API documentation. It is also a great tool to test the APIs.

We already had built Spring Boot RESTful WebService earlier. We will add Swagger 2 to it.

Dependencies

Add springfox-swagger2 and springfox-swagger-ui dependencies.

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
Changes in SpringBootApplication class
  1. Add @EnableSwagger2 annotation in the SpringBootApplication class
  2. Define bean for Docket api.
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spring.web.plugins.Docket;
@SpringBootApplication
@Configuration
@ComponentScan(“com.spring.rest”)
@PropertySource(“classpath:database.properties”)
@EnableSwagger2
public class Application {
@Autowired
Environment environment;
private final String URL = “url”;
private final String USER = “dbuser”;
private final String DRIVER = “driver”;
private final String PASSWORD = “dbpassword”;
@Bean
DataSource dataSource() {
DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
driverManagerDataSource.setUrl(environment.getProperty(URL));
driverManagerDataSource.setUsername(environment.getProperty(USER));
driverManagerDataSource.setPassword(environment.getProperty(PASSWORD));
driverManagerDataSource.setDriverClassName(environment.getProperty(DRIVER));
return driverManagerDataSource;
}
@Bean
JdbcTemplate jdbcTemplate() {
return new JdbcTemplate(dataSource());
}
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).select()
.apis(RequestHandlerSelectors.basePackage(“com.spring.rest.controller”)).build();
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Execute the main method of Application class and hit http://localhost:8080/swagger-ui.html in browser.
Screen Shot 2020-02-29 at 8.28.08 PM
Expand Models Tab
Screen Shot 2020-02-29 at 8.28.33 PM
We can invoke the RESTful APIs in Swagger 2.
Screen Shot 2020-02-29 at 8.29.59 PM

Leave a Reply

Your email address will not be published. Required fields are marked *