SpringCloud(8)zuul與Swagger的整合

Swagger

它可以輕鬆的整合到Spring Boot中,並與Spring MVC程序配合組織出強大RESTful API文檔。它既可以減少我們創建文檔的工作量,同時說明內容又整合入實現代碼中,讓維護文檔和修改代碼整合爲一體,可以讓我們在修改代碼邏輯的同時方便的修改文檔說明。另外Swagger2也提供了強大的頁面測試功能來調試每個RESTful API。

在zuul工程中配置Swagger

  1. 在pom.xml文件中加入以下依賴

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.6.1</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.6.1</version>
    </dependency>
    
  2. Swagger的配置

    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
    @Bean
    UiConfiguration uiConfiguration(){
        return new UiConfiguration(null, "list", "alpha", "schema",
            UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS, true, true, 60000L);
    }
     @Bean
     Docket createRestApi(){
            return new Docket(DocumentationType.SWAGGER_2).apiInfo(
                new ApiInfoBuilder()
                        .title("分佈式Api管理").description("分佈式文檔管理")
                .termsOfServiceUrl("http://localhost:86")
            .version("1.0").build()
            );
         }
    }
    
  3. 加入列表

    @Component
    @Primary
    public class SwaggerResoureConfig implements SwaggerResourcesProvider {
    @Override
    public List<SwaggerResource> get() {
        List<SwaggerResource> swaggerResoures=new ArrayList<>();
        swaggerResoures.add(getSwaggerResoure("a","/api-a/v2/api-docs","2.0"));
        swaggerResoures.add(getSwaggerResoure("b","/api-b/v2/api-docs","2.0"));
        return swaggerResoures;
    }
    
    private  SwaggerResource getSwaggerResoure(String name,String loc,String version){
            SwaggerResource swaggerResource=new SwaggerResource();
            swaggerResource.setName(name);
            swaggerResource.setLocation(loc);
            swaggerResource.setSwaggerVersion(version);
            return swaggerResource;
        }
    }   
    
  4. 在你需要生成restful API文檔下 也是上面一樣的配置,但是少了第三步配置

    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
        @Bean
        Docket createRestApi(){
            return new Docket(DocumentationType.SWAGGER_2).apiInfo(new ApiInfoBuilder()
                    .title("a").version("1.0").contact(new Contact("tc","","[email protected]"))
                    .description("a系統的接口文檔").build()).select()
                    .apis(RequestHandlerSelectors.basePackage("com.tc.eurekahelloconsume.controller"))
                    .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                    .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                    .build();
        }
        @Bean
        UiConfiguration uiConfig() {
            return new UiConfiguration(null, "list", "alpha", "schema",
                    UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS, true, true, 60000L);
        }
    }
    
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章