In my spring boot application, I have multiple Rest Controllers and need to generate swagger for each controller seperately.
By using below Docket config for each co
You can add multiple controller class using following Swagger Configuration:
1) Create a Swagger Configuration Class.
2) Then specify the base package of controllers.
import java.util.Collections;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig
{
private static final ApiInfo DEFAULT_API_INFO = null; //Swagger info
@Bean
public Docket api()
{
return new Docket(DocumentationType.SWAGGER_2)
.forCodeGeneration(Boolean.TRUE)
.select()
.apis(RequestHandlerSelectors.basePackage("com.user.controller"))
.paths(PathSelectors.any())
.paths(Predicates.not(PathSelectors.regex("/logout.*")))
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfo(
"REST API",
"REST description of API.",
"API TOS",
"Terms of service",
new Contact("Rajib Garai", "https://www.linkedin.com/in/rajibgarai90/", "90rajibgarai@gmail.com"),
"License of API", "API license URL", Collections.emptyList());
}
}
Here is the code i wrote to find and automatically create Docket on runtime per controller, also has a Default Docket to show all in one group.
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Autowired
ConfigurableApplicationContext context;
//Default Docket to show all
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(metaData())
.forCodeGeneration(Boolean.TRUE)
.select()
.apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
.paths(PathSelectors.any())
.paths(Predicates.not(PathSelectors.regex("/error.*")))
.build();
}
//Creating Docket Dynamically per Rest Controller
@PostConstruct
public void postConstruct() throws ClassNotFoundException {
ClassPathScanningCandidateComponentProvider provider
= new ClassPathScanningCandidateComponentProvider(false);
provider.addIncludeFilter(new AnnotationTypeFilter(RestController.class));
for (BeanDefinition beanDef : provider.findCandidateComponents("com.blah.blah.package")) {
Class<?> cl = Class.forName(beanDef.getBeanClassName());
RequestMapping requestMapping = cl.getAnnotation(RequestMapping.class);
if (null != requestMapping && null != requestMapping.value() && requestMapping.value().length > 0) {
String resource_group = requestMapping.value()[0];
SingletonBeanRegistry beanRegistry = context.getBeanFactory();
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.groupName(resource_group)
.apiInfo(metaData())
.forCodeGeneration(Boolean.TRUE)
.select()
//.apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
.paths(PathSelectors.regex(resource_group + ".*"))
.paths(Predicates.not(PathSelectors.regex("/error.*")))
.build();
beanRegistry.registerSingleton(cl.getSimpleName() + "_docket_api", docket);
}
}
}
private ApiInfo metaData() {
return new ApiInfoBuilder()
.title("some Title Here")
.description("Some Desciption")
.version("1.0")
.contact(new Contact("Asad Abdin", "", "asadabdin@gmail.com"))
.build();
}