Spring 配置Swagger


 

 

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配置會產生此問題。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM