Zuul整合Swagger2与swagger-bootstrap-ui

2024-05-02 03:18

本文主要是介绍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.测试效果
image.png
image.png

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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/953276

相关文章

SpringBoot 整合 Grizzly的过程

《SpringBoot整合Grizzly的过程》Grizzly是一个高性能的、异步的、非阻塞的HTTP服务器框架,它可以与SpringBoot一起提供比传统的Tomcat或Jet... 目录为什么选择 Grizzly?Spring Boot + Grizzly 整合的优势添加依赖自定义 Grizzly 作为

Python中的可视化设计与UI界面实现

《Python中的可视化设计与UI界面实现》本文介绍了如何使用Python创建用户界面(UI),包括使用Tkinter、PyQt、Kivy等库进行基本窗口、动态图表和动画效果的实现,通过示例代码,展示... 目录从像素到界面:python带你玩转UI设计示例:使用Tkinter创建一个简单的窗口绘图魔法:用

springboot整合gateway的详细过程

《springboot整合gateway的详细过程》本文介绍了如何配置和使用SpringCloudGateway构建一个API网关,通过实例代码介绍了springboot整合gateway的过程,需要... 目录1. 添加依赖2. 配置网关路由3. 启用Eureka客户端(可选)4. 创建主应用类5. 自定

element-ui下拉输入框+resetFields无法回显的问题解决

《element-ui下拉输入框+resetFields无法回显的问题解决》本文主要介绍了在使用ElementUI的下拉输入框时,点击重置按钮后输入框无法回显数据的问题,具有一定的参考价值,感兴趣的... 目录描述原因问题重现解决方案方法一方法二总结描述第一次进入页面,不做任何操作,点击重置按钮,再进行下

springboot整合 xxl-job及使用步骤

《springboot整合xxl-job及使用步骤》XXL-JOB是一个分布式任务调度平台,用于解决分布式系统中的任务调度和管理问题,文章详细介绍了XXL-JOB的架构,包括调度中心、执行器和Web... 目录一、xxl-job是什么二、使用步骤1. 下载并运行管理端代码2. 访问管理页面,确认是否启动成功

SpringBoot3集成swagger文档的使用方法

《SpringBoot3集成swagger文档的使用方法》本文介绍了Swagger的诞生背景、主要功能以及如何在SpringBoot3中集成Swagger文档,Swagger可以帮助自动生成API文档... 目录一、前言1. API 文档自动生成2. 交互式 API 测试3. API 设计和开发协作二、使用

SpringBoot整合kaptcha验证码过程(复制粘贴即可用)

《SpringBoot整合kaptcha验证码过程(复制粘贴即可用)》本文介绍了如何在SpringBoot项目中整合Kaptcha验证码实现,通过配置和编写相应的Controller、工具类以及前端页... 目录SpringBoot整合kaptcha验证码程序目录参考有两种方式在springboot中使用k

Spring Boot 中整合 MyBatis-Plus详细步骤(最新推荐)

《SpringBoot中整合MyBatis-Plus详细步骤(最新推荐)》本文详细介绍了如何在SpringBoot项目中整合MyBatis-Plus,包括整合步骤、基本CRUD操作、分页查询、批... 目录一、整合步骤1. 创建 Spring Boot 项目2. 配置项目依赖3. 配置数据源4. 创建实体类

SpringBoot整合InfluxDB的详细过程

《SpringBoot整合InfluxDB的详细过程》InfluxDB是一个开源的时间序列数据库,由Go语言编写,适用于存储和查询按时间顺序产生的数据,它具有高效的数据存储和查询机制,支持高并发写入和... 目录一、简单介绍InfluxDB是什么?1、主要特点2、应用场景二、使用步骤1、集成原生的Influ

SpringBoot整合Canal+RabbitMQ监听数据变更详解

《SpringBoot整合Canal+RabbitMQ监听数据变更详解》在现代分布式系统中,实时获取数据库的变更信息是一个常见的需求,本文将介绍SpringBoot如何通过整合Canal和Rabbit... 目录需求步骤环境搭建整合SpringBoot与Canal实现客户端Canal整合RabbitMQSp