SpringCloud zull 整合Swagger
-
添加依赖
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.7.0</version> </dependency>
-
创建配置类
package com.exchange.user.utils; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger.web.UiConfiguration; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration public class Swagger2Config { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.exchange.user.controller")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("用户模块") .description("endless") .termsOfServiceUrl("http://www.baidu.com/") .contact("xydeveloper@126.com") .version("1.0") .build(); } @Bean protected UiConfiguration uiConfig() { return new UiConfiguration(null, "list", "alpha", "schema", UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS, false, true, 60000L); }
}
```
-
启动类加注解
package com.exchange.user; import org.springframework.boot.SpringApplication; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cloud.client.SpringCloudApplication; import org.springframework.scheduling.annotation.EnableScheduling; import springfox.documentation.swagger2.annotations.EnableSwagger2; import tk.mybatis.spring.annotation.MapperScan; @EnableCaching @EnableSwagger2 @EnableScheduling @SpringCloudApplication @MapperScan("com.exchange.user.mapper") public class UserApplication { public static void main(String[] args) { SpringApplication.run(UserApplication.class); } }
- 网关添加Swagger整合各个子模块的总配置文件
package com.exchange.gateway.config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Component; import springfox.documentation.swagger.web.SwaggerResource; import springfox.documentation.swagger.web.SwaggerResourcesProvider; import java.util.ArrayList; import java.util.List; @Component @Primary public class SwaggerUniteConfig implements SwaggerResourcesProvider { @Value("${PREFIX}") private String PREFIX; @Value("${SUFFIX}") private String SUFFIX; @Value("${VERSION}") private String VERSION; @Value("${REMOVE_SERVER_NAME}") private String REMOVE_SERVER_NAME; @Value("${SERVER_PREFIX}") private String SERVER_PREFIX; @Autowired private DiscoveryClient discoveryClient; @Override public List<SwaggerResource> get() { List listSwaggerUi = new ArrayList<>(); discoveryClient.getServices().forEach(serverName -> { SwaggerResource swaggerResource = new SwaggerResource(); swaggerResource.setSwaggerVersion(VERSION); if (!(SERVER_PREFIX + REMOVE_SERVER_NAME).equals(serverName)) { serverName = serverName.replace(SERVER_PREFIX, ""); swaggerResource.setName(serverName); swaggerResource.setLocation(PREFIX + serverName + SUFFIX); listSwaggerUi.add(swaggerResource); } }); return listSwaggerUi; } }
- 网关appcation常量
spring: application: name: exchange-gateway eureka: client: service-url: defaultZone: http://127.0.0.1:9000/eureka/ registry-fetch-interval-seconds: 5 instance: prefer-ip-address: true ip-address: 127.0.0.1 zuul: prefix: /exchange routes: exchange-user: /user/** exchange-manage: /manage/** exchange-trade-coin2coin: /coin2coin/** exchange-sto: /sto/** exchange-storage: /storage/** exchange-system: /system/** exchange-sto-manage: /sto-manage/** exchange-agent: /agent/** exchange-o2o: /o2o/** exchange-user-app: /user-app/** exchange-sto-app: /sto-app/** hystrix: command: default: execution: isolation: thread: timeoutInMilliseconds: 70000 ribbon: connetTimeout: 60000 readTimeout: 60000 PREFIX: /exchange/ SUFFIX: /v2/api-docs VERSION: 1.0 REMOVE_SERVER_NAME: gateway SERVER_PREFIX: exchange-