Steps to integrate Swagger Documentation with Spring Boot Application:
Step-1: Dependency addition in Maven
Step-2: Enable Swagger in your application
Use the following annotations(highlighted in bold) in your Spring Boot Application java file:
@SpringBootApplication
@EnableSwagger2
@ComponentScan(basePackages={"com.test.controller"})
public class SpringBootApp extends SpringBootServletInitializer {
-------------
-------------
}
@ComponentScan will be used to inform swagger which package to scan to generate documentation.
My Complete Boot Application file looks as follows:
import static springfox.documentation.builders.PathSelectors.regex;
import java.util.Properties;
import org.apache.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@SpringBootApplication
@EnableSwagger2
@ComponentScan(basePackages={"com.test.controller"})
public class SmartGateApplication extends SpringBootServletInitializer {
final Logger log = Logger.getLogger(getClass().getName());
public static void main(String[] args) throws Exception {
SpringApplication.run(SmartGateApplication.class, args);
}
public void run(String... args) throws Exception {
log.info("Application Started Successfully !!!");
}
@Bean
public Docket newsApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.test.controller")) //Controller's base path
.paths(regex("/test.*")) // This URL is the parent path of service endpoint
.build();
}
@SuppressWarnings("deprecation")
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Swagger documentation my REST Application")
.description("Documentation for REST services")
.termsOfServiceUrl("www.test.ae")
.contact("dev@test.come")
.license("DevTeam")
.licenseUrl("www.test.ae")
.version("1.0")
.build();
}
}
Step-1: Dependency addition in Maven
<
dependency
>
<
groupId
>io.springfox</
groupId
>
<
artifactId
>springfox-swagger2</
artifactId
>
<
version
>2.6.1</
version
>
</
dependency
>
Step-2: Enable Swagger in your application
Use the following annotations(highlighted in bold) in your Spring Boot Application java file:
@SpringBootApplication
@EnableSwagger2
@ComponentScan(basePackages={"com.test.controller"})
public class SpringBootApp extends SpringBootServletInitializer {
-------------
-------------
}
@ComponentScan will be used to inform swagger which package to scan to generate documentation.
My Complete Boot Application file looks as follows:
import static springfox.documentation.builders.PathSelectors.regex;
import java.util.Properties;
import org.apache.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@SpringBootApplication
@EnableSwagger2
@ComponentScan(basePackages={"com.test.controller"})
public class SmartGateApplication extends SpringBootServletInitializer {
final Logger log = Logger.getLogger(getClass().getName());
public static void main(String[] args) throws Exception {
SpringApplication.run(SmartGateApplication.class, args);
}
public void run(String... args) throws Exception {
log.info("Application Started Successfully !!!");
}
@Bean
public Docket newsApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.test.controller")) //Controller's base path
.paths(regex("/test.*")) // This URL is the parent path of service endpoint
.build();
}
@SuppressWarnings("deprecation")
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Swagger documentation my REST Application")
.description("Documentation for REST services")
.termsOfServiceUrl("www.test.ae")
.contact("dev@test.come")
.license("DevTeam")
.licenseUrl("www.test.ae")
.version("1.0")
.build();
}
}
Step-3
That's it. Now our setup is ready and we can start the server and hit the following URL:
http://localhost:8080/swagger-ui.html
You can also generate the documentation in JSON format using the following URL
http://localhost:8080/v2/api-docs
If you face any trouble, please mail me.
Comments
Post a Comment