springCloud中基於Zuul整合Swagger2(採用boostrapUI,界面好看)

引入以下依賴:html

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>

    <dependency>
        <groupId>com.github.xiaoymin</groupId>
        <artifactId>swagger-bootstrap-ui</artifactId>
        <version>1.8.7</version>
    </dependency>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>25.1-jre</version>
    </dependency>

在zuul模塊中配置SwaggerResourcesProvidergit

@Component
 @Primary
public class GatewaySwaggerResourcesProvider implements SwaggerResourcesProvider {
    private final RouteLocator routeLocator;

    public GatewaySwaggerResourcesProvider(RouteLocator routeLocator) {
        this.routeLocator = routeLocator;
    }

    @Override
    public List<SwaggerResource> get() {
        List<SwaggerResource> resources = new ArrayList<>();
        List<Route> routes = routeLocator.getRoutes();
        for (Route route:routes) {
            resources.add(swaggerResource(route.getId(), route.getFullPath().replace("**", "v2/api-docs")));
        }
        return resources;
    }
    private SwaggerResource swaggerResource(String name, String location) {
        SwaggerResource swaggerResource = new SwaggerResource();
        swaggerResource.setName(name);
        swaggerResource.setLocation(location);
        swaggerResource.setSwaggerVersion("2.0");
        return swaggerResource;
    }
}

 在zuul模塊中配置SwaggerConfiggithub

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo());
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("說明文檔")
                .description("接口說明文檔")
                .termsOfServiceUrl("")
                .contact(new Contact("xhx","",""))
                .version("1.0")
                .build();
    }
}

在具體的服務模塊中添加SwaggerConfigspring

@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
      return new Docket(DocumentationType.SWAGGER_2)
                        .apiInfo(apiInfo())
                        .select()
                         .apis(RequestHandlerSelectors.basePackage("com.dfove.dakx.auth.client.controller"))
                            //basePage就是具體要掃描的包,有swagger註解的方法就生成到文檔裏面去
                        .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                         .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                         .paths(PathSelectors.any())
                        .build();
             }
    private ApiInfo apiInfo() {
     return new ApiInfoBuilder()
                        .title("用戶系統api")
                         .description("用戶系統接口文檔說明")
                        .contact(new Contact("xhx", "", ""))
                         .version("1.0")
                         .build();
             }
            @Bean
     UiConfiguration uiConfig() {
                 return new UiConfiguration(null, "list", "alpha", "schema",
                                 UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS, false, true, 60000L);
             }
 }

配置完成以後,在須要的接口上加上swagger的註解就能夠將接口生成到文檔裏面了;bootstrap

輸入http://localhost:8060/doc.html查看界面,這裏選用swagger-bootstrap-ui,界面好看不少,端口號爲網關端口號api

相關文章
相關標籤/搜索