swagger配置示例:其中,titile的內容會顯示在swagger頁面頂部,RequestHandlerSelectors.basePackage和PathSelectors.any從包結構和api路徑兩個方面篩選要顯示的api接口
@Component
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(new ApiInfoBuilder()
.title("title")
.build())
.select()
.apis(RequestHandlerSelectors.basePackage("BUSINESS_PATH"))
.paths(PathSelectors.any())
.build();
}
}
@EnableSwagger2Doc 此注解會顯示Spring內部的api。一般不使用此注解。
異常解決:
1、
org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.IllegalStateException: Multiple Dockets with the same group name are not supported. The following duplicate groups were discovered. default
Caused by: java.lang.IllegalStateException: Multiple Dockets with the same group name are not supported. The following duplicate groups were discovered. default
at springfox.documentation.spring.web.plugins.DuplicateGroupsDetector.ensureNoDuplicateGroups(DuplicateGroupsDetector.java:45)
這個問題是Swagger配置重復定義,@EnableSwagger2Doc注解也會定義一個swagger,使用@EnableSwagger2Doc的同時再自定義swagger配置會產生此問題。
