SpringBoot整合Swagger2

首先遵循SpringBoot的三板斧

第一步添加依賴

<!-- SwaggerUI 接口文檔 http://{ip}:{prot}/swagger-ui.html -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>{version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>{version}</version>
</dependency>

第二步添加註解

@EnableSwagger2 //啓動SwaggerUI,在啓動類或Swagger配置類上添加該註解

第三步寫配置

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket api() {

/*
        //可以添加多個header或參數
        ParameterBuilder aParameterBuilder = new ParameterBuilder();
        aParameterBuilder
                //參數類型支持header, cookie, body, query etc
                .parameterType("header")
                //參數名
                .name("user-token")
                //默認值
                .defaultValue("t122222")
                .description("用戶登錄憑證")
                //指定參數值的類型
                .modelRef(new ModelRef("string"))
                //非必需,這裏是全局配置
                .required(false).build();
        List<Parameter> aParameters = new ArrayList<>();
        aParameters.add(aParameterBuilder.build());
*/

        return new Docket(DocumentationType.SWAGGER_2)
//        return new Docket(DocumentationType.SPRING_WEB)
                .apiInfo(apiInfo())
                .pathMapping("/")
                .select()// 選擇那些路徑和api會生成document
                .apis(RequestHandlerSelectors.any())// 對所有api進行監控
                // 不顯示錯誤的接口地址
                .paths(Predicates.not(PathSelectors.regex("/error.*")))// 錯誤error路徑不監控
                .paths(Predicates.not(PathSelectors.regex("/actuator.*")))// 錯誤error路徑不監控
                .paths(PathSelectors.regex("/.*"))// 對根下所有路徑進行監控
                .paths(PathSelectors.any())   // 對所有路徑進行監控
                // 自行修改爲自己的包路徑
//                .apis(RequestHandlerSelectors.basePackage("com.happyloves.zc.service.account.api"))
                .build()
//                .globalOperationParameters(aParameters)
                .enable(true);
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("API接口")
                .description("API接口文檔")
                //服務條款網址
//                .termsOfServiceUrl("https://www.google.com")
                .version("1.0")
//                .contact(new Contact("啦啦啦", "url", "email"))
                .build();
    }
}

擴展:swagger-bootstrap-ui是springfox-swagger的增強UI實現,爲Java開發者在使用Swagger的時候,能擁有一份簡潔、強大的接口文檔體驗
項目地址
碼雲:https://gitee.com/xiaoym/swag...

GitHub:https://github.com/xiaoymin/S...

在線體驗:http://swagger-bootstrap-ui.x...

項目文檔:http://www.xiaominfo.com/swag...

代碼集成示例

SpringBoot在線demo地址:https://gitee.com/xiaoym/swag...

Spring Mvc在線demo地址:https://gitee.com/xiaoym/swag...

添加依賴

<!-- swagger-bootstrap-ui是 Swagger 的增強UI 實現,使文檔更友好一點兒 http://{ip}:{prot}/doc.html -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.6</version>
</dependency>

趙小胖個人博客

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章