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

相关文章

HarmonyOS学习(七)——UI(五)常用布局总结

自适应布局 1.1、线性布局(LinearLayout) 通过线性容器Row和Column实现线性布局。Column容器内的子组件按照垂直方向排列,Row组件中的子组件按照水平方向排列。 属性说明space通过space参数设置主轴上子组件的间距,达到各子组件在排列上的等间距效果alignItems设置子组件在交叉轴上的对齐方式,且在各类尺寸屏幕上表现一致,其中交叉轴为垂直时,取值为Vert

RabbitMQ使用及与spring boot整合

1.MQ   消息队列(Message Queue,简称MQ)——应用程序和应用程序之间的通信方法   应用:不同进程Process/线程Thread之间通信   比较流行的中间件:     ActiveMQ     RabbitMQ(非常重量级,更适合于企业级的开发)     Kafka(高吞吐量的分布式发布订阅消息系统)     RocketMQ   在高并发、可靠性、成熟度等

springboot整合swagger2之最佳实践

来源:https://blog.lqdev.cn/2018/07/21/springboot/chapter-ten/ Swagger是一款RESTful接口的文档在线自动生成、功能测试功能框架。 一个规范和完整的框架,用于生成、描述、调用和可视化RESTful风格的Web服务,加上swagger-ui,可以有很好的呈现。 SpringBoot集成 pom <!--swagge

springboot 整合swagger

没有多余废话,就是干 spring-boot 2.7.8 springfox-boot-starter 3.0.0 结构 POM.xml <?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/

Golang GUI入门——andlabs ui

官方不提供gui标准库,只好寻求第三方库。 https://github.com/google/gxui 这个gui库是谷歌内部人员提供的,并不是谷歌官方出品,现在停止维护,只好作罢。 第三方gui库 找了好多,也比较了好多,最终决定使用的是还是 https://github.com/andlabs/ui 相信golang gui还会发展的更好,期待更优秀的gui库 由于andlabs

Zuul详细解释

Zuul 是 Netflix 开源的 API 网关,广泛用于微服务架构中。它作为系统的前置网关,主要功能包括路由、负载均衡、限流、安全性管理等。Zuul 最常见的应用场景是作为反向代理,它接收所有来自客户端的请求,并将请求转发给后端的微服务,从而屏蔽了微服务的复杂性。Spring Cloud 集成了 Zuul,使其成为 Spring Cloud 微服务生态系统中的一个重要组件。 为什么使用 Zu

Java后端微服务架构下的服务网关设计:Spring Cloud Zuul

Java后端微服务架构下的服务网关设计:Spring Cloud Zuul 大家好,我是微赚淘客返利系统3.0的小编,是个冬天不穿秋裤,天冷也要风度的程序猿! 在微服务架构中,服务网关是微服务系统与外部世界的入口点,它负责请求路由、负载均衡、认证、监控等任务。Spring Cloud Zuul是一个基于Spring Boot的网关服务,它为微服务架构提供了一种灵活、高效的网关解决方案。 服务

uniapp,vite整合windicss

官方文档:https://weapp-tw.icebreaker.top/docs/quick-start/frameworks/hbuilderx 安装: npm i -D tailwindcss postcss autoprefixer# 初始化 tailwind.config.js 文件npx tailwindcss initnpm i -D weapp-tailwindcss# 假

Spring Cloud整合Seata实现分布式事务

文章目录 1.Seata1.1 官网1.2 下载1.3 通过安装包运行seata1.3.1 解压seata-server-1.3.0.zip1.3.2 修改 conf/file.conf 配置文件1.3.3 修改conf/registry.conf配置文件1.3.4 添加seata配置信息到nacos1.3.5 配置seata服务端数据库表结构1.3.6 启动seata 2.Spring

在项目SSH整合时,启动测试时报的警告!

一、tomcat 运行过程中提示: 22:11:07,082 [main] WARN  DTDEntityResolver : recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer