本文主要是介绍Zuul整合Swagger2与swagger-bootstrap-ui,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、项目情况
1、Eureka项目:shop-eurake
1.1 启动类
@SpringBootApplication
@EnableEurekaServer
public class ShopEurekaApplication {public static void main(String[] args) {SpringApplication.run(ShopEurekaApplication.class, args);}
}
1.2 资源文件
server.port=8000
spring.application.name=shop-eureka
2、Zuul项目:shop-gateway
2.1 启动类
@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
public class ShopGatewayApplication {public static void main(String[] args) {SpringApplication.run(ShopGatewayApplication.class, args);}
}
2.2 pom文件,需要添加swagger2相关包
<dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.9.2</version>
</dependency>
<dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.9.2</version>
</dependency>
<dependency><groupId>com.github.xiaoymin</groupId><artifactId>swagger-bootstrap-ui</artifactId><version>1.9.2</version>
</dependency>
<dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId><version>20.0</version>
</dependency>
2.3 配置文件GatewaySwaggerResourcesProvider
import java.util.ArrayList;
import java.util.List;
import org.springframework.cloud.netflix.zuul.filters.Route;
import org.springframework.cloud.netflix.zuul.filters.RouteLocator;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;
import springfox.documentation.swagger2.annotations.EnableSwagger2;@Component
@Primary
@EnableSwagger2
public class GatewaySwaggerResourcesProvider implements SwaggerResourcesProvider {private final RouteLocator routeLocator;public GatewaySwaggerResourcesProvider(RouteLocator routeLocator) {this.routeLocator = routeLocator;}@Overridepublic 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"),"2.0"));}return resources;}private SwaggerResource swaggerResource(String name, String location, String version) {SwaggerResource swaggerResource = new SwaggerResource();swaggerResource.setName(name);swaggerResource.setLocation(location);swaggerResource.setSwaggerVersion(version);return swaggerResource;}
}
2.4 配置文件WebMvcConfig
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;import springfox.documentation.spring.web.SpringfoxWebMvcConfiguration;@SpringBootApplication
@ConditionalOnClass(SpringfoxWebMvcConfiguration.class)
public class WebMvcConfig extends WebMvcConfigurationSupport{@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");//doc.html是文档的访问路径,可以自己更改registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");}
}
2.5 资料文件
server.port=8001
spring.application.name=shop-gateway
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
3、业务项目1:shop-business
3.1 pom依赖,不需要swaggerUI
<dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.9.2</version>
</dependency>
<dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId>
</dependency>
3.2 启动类
@SpringBootApplication
@EnableEurekaClient
public class ShopBusinessApplication {public static void main(String[] args) {SpringApplication.run(ShopBusinessApplication.class, args);}}
3.3 Swagger配置文件Swagger2,注意basePackage是你的controller的包目录不能错
@Configuration
@EnableSwagger2
public class Swagger2 {@Beanpublic Docket createRestApi() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage("com.slan.air.shop.controller")).paths(PathSelectors.any()).build();}private ApiInfo apiInfo() {return new ApiInfoBuilder().title("购物车-业务模块")//项目模块描述.description("slan").version("1.0").build();}
}
3.4 资源文件
server.port=8013
spring.application.name=shop-business
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8000/eureka/
3.5 需要展示的接口类OrderController
@RestController
public class OrderController {@ApiOperation(value = "商品详情", httpMethod = "GET")@RequestMapping(value="/get/{id}", method = RequestMethod.GET)public String get(@PathVariable String id) {try {System.out.println(LocalTime.now());} catch (Exception e) {e.printStackTrace();}return id+"";}
}
4、业务项目2:shop-user-center
4.1 pom依赖,不需要swaggerUI
<dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.9.2</version>
</dependency>
<dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId>
</dependency>
4.2 启动类
@SpringBootApplication
@EnableEurekaClient
public class ShopUserCenterApplication {public static void main(String[] args) {SpringApplication.run(ShopUserCenterApplication.class, args);}
}
4.3 Swagger配置文件Swagger2,注意basePackage是你的controller的包目录不能错
@Configuration
@EnableSwagger2
public class Swagger2 {@Beanpublic Docket createRestApi() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage("com.slan.air.shop.controller")).paths(PathSelectors.any()).build();}private ApiInfo apiInfo() {return new ApiInfoBuilder().title("购物车 -用户模块AIP")//项目模块描述.description("slan").version("1.0").build();}
}
4.4 资源文件
server.port=8002
spring.application.name=shop-user-center
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8000/eureka/
4.5 需要展示的接口类OrderController
@Api(value = "用户信息管理")
@RestController
public class UserController {@ApiOperation(value = "获取用户详细信息", notes="获取用户详细信息")@ApiImplicitParam(name = "id", value = "用户ID", paramType = "path", required = true, dataType = "Integer")@RequestMapping(value = "/get/{id}" ,method = RequestMethod.GET)public String getUser(@PathVariable Integer id) {try {System.out.println(LocalTime.now());} catch (InterruptedException e) {e.printStackTrace();}return id+"";}
}
二、测试情况
1.访问路径
http://ip:网关端口/WebMvcConfig类中的addResourceHandler(“doc.html”),
如:http://localhost:8001/doc.html
2.测试效果
3.通过上图可以发现所有注册到eureka的服务都被加入进来了,不需要每个服务都要配置swaggerui,也不需要到处找接口文档了。
4.通过swaggerui上可以直接复制markdown格式文件。
三、总结
1.注意com.google.guava包版本问题,如果版本过低会报异常:
org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is com.google.common.util.concurrent.ExecutionError: java.lang.NoSuchMethodError: com.google.common.collect.FluentIterable.concat(Ljava/lang/Iterable;Ljava/lang/Iterable;)Lcom/google/common/collect/FluentIterable;at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:178) ~[spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE]at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:50) ~[spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE]at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:348) ~[spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE]at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:151) ~[spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE]at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:114) ~[spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE]at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:880) ~[spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE]at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:144) ~[spring-boot-1.4.7.RELEASE.jar:1.4.7.RELEASE]at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546) ~[spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE]at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.4.7.RELEASE.jar:1.4.7.RELEASE]at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762) [spring-boot-1.4.7.RELEASE.jar:1.4.7.RELEASE]at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:372) [spring-boot-1.4.7.RELEASE.jar:1.4.7.RELEASE]at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-1.4.7.RELEASE.jar:1.4.7.RELEASE]at org.springframework.boot.SpringApplication.run(SpringApplication.java:1187) [spring-boot-1.4.7.RELEASE.jar:1.4.7.RELEASE]at org.springframework.boot.SpringApplication.run(SpringApplication.java:1176) [spring-boot-1.4.7.RELEASE.jar:1.4.7.RELEASE]at com.slan.air.shop.ShopGatewayApplication.main(ShopGatewayApplication.java:14) [classes/:na]
Caused by: com.google.common.util.concurrent.ExecutionError: java.lang.NoSuchMethodError: com.google.common.collect.FluentIterable.concat(Ljava/lang/Iterable;Ljava/lang/Iterable;)Lcom/google/common/collect/FluentIterable;at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2201) ~[guava-18.0.jar:na]at com.google.common.cache.LocalCache.get(LocalCache.java:3937) ~[guava-18.0.jar:na]at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3941) ~[guava-18.0.jar:na]at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4824) ~[guava-18.0.jar:na]at springfox.documentation.schema.CachingModelDependencyProvider.dependentModels(CachingModelDependencyProvider.java:58) ~[springfox-schema-2.9.2.jar:null]at springfox.documentation.schema.DefaultModelProvider.dependencies(DefaultModelProvider.java:128) ~[springfox-schema-2.9.2.jar:null]at springfox.documentation.schema.CachingModelProvider.dependencies(CachingModelProvider.java:68) ~[springfox-schema-2.9.2.jar:null]at springfox.documentation.spring.web.scanners.ApiModelReader.populateDependencies(ApiModelReader.java:136) ~[springfox-spring-web-2.9.2.jar:null]at springfox.documentation.spring.web.scanners.ApiModelReader.read(ApiModelReader.java:78) ~[springfox-spring-web-2.9.2.jar:null]at springfox.documentation.spring.web.scanners.ApiListingScanner.scan(ApiListingScanner.java:133) ~[springfox-spring-web-2.9.2.jar:null]at springfox.documentation.spring.web.scanners.ApiDocumentationScanner.scan(ApiDocumentationScanner.java:71) ~[springfox-spring-web-2.9.2.jar:null]at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.scanDocumentation(DocumentationPluginsBootstrapper.java:101) ~[springfox-spring-web-2.9.2.jar:null]at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.start(DocumentationPluginsBootstrapper.java:167) ~[springfox-spring-web-2.9.2.jar:null]at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:175) ~[spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE]... 14 common frames omitted
Caused by: java.lang.NoSuchMethodError: com.google.common.collect.FluentIterable.concat(Ljava/lang/Iterable;Ljava/lang/Iterable;)Lcom/google/common/collect/FluentIterable;at springfox.documentation.schema.DefaultModelDependencyProvider.dependentModels(DefaultModelDependencyProvider.java:79) ~[springfox-schema-2.9.2.jar:null]at springfox.documentation.schema.CachingModelDependencyProvider$1.load(CachingModelDependencyProvider.java:50) ~[springfox-schema-2.9.2.jar:null]at springfox.documentation.schema.CachingModelDependencyProvider$1.load(CachingModelDependencyProvider.java:48) ~[springfox-schema-2.9.2.jar:null]at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3527) ~[guava-18.0.jar:na]at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2319) ~[guava-18.0.jar:na]at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2282) ~[guava-18.0.jar:na]at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2197) ~[guava-18.0.jar:na]... 27 common frames omitted
这篇关于Zuul整合Swagger2与swagger-bootstrap-ui的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!