Spring Cloud Gateway 解决使用动态路由后swagger失效

使用org.springframework.cloud.gateway.route.RouteDefinitionLocator获取路由信息,并封装到
springfox.documentation.swagger.web.SwaggerResource

 

import org.springframework.cloud.gateway.route.RouteDefinitionLocator;
import org.springframework.cloud.gateway.support.NameUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 向振华
 * @date 2020/09/16 09:52
 */
@Primary
@Component
public class SwaggerProvider implements SwaggerResourcesProvider {

    public static final String API_URI = "/v2/api-docs";

    @Resource
    private RouteDefinitionLocator routeDefinitionLocator;

    
Spring Cloud Gateway 2.3.12 是 Spring Cloud 的一个组件,它是一个高度可配置的 HTTP 代理服务器,用于微服务架构中的API网关。要将 Swagger(通常指Swagger UI)集成到 Spring Cloud Gateway 中,你可以按照以下步骤操作: 1. 添加依赖:在你的项目中添加 SwaggerSwagger UI 的依赖。如果你使用 Maven,可以在 `pom.xml` 文件中添加: ```xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter-web</artifactId> <version>3.x.x</version> <!-- 使用最新稳定版本 --> </dependency> <dependency> <groupId>io.github.swagger2markup</groupId> <artifactId>swagger2markup</artifactId> <version>1.0.0-M7</version> </dependency> ``` 注意:替换 `<version>` 部分为当前最稳定的 Swagger 版本。 2. 创建配置:在 `application.yml` 或 `application.properties` 中配置 Swagger 的信息,例如: ```yaml # application.yml springdoc: webFlux: openapi: info: title: 'Your API Title' version: 'v1' description: 'Description of your API' # 如果想要自定义路径和端点,可以添加这些配置 springdoc.webFlux.path=/docs, /api-docs springdoc.webFlux.endpoint-path=/v2/api-docs # Swagger UI 配置 springdoc.swagger-ui.enabled=true ``` 3. 注解控制器:在你的 API 控制器上添加 Swagger 注解,如 `@ApiOperation` 和 `@ApiResponses`。这会告诉 Swagger 生成文档的内容。 4. 自动扫描:确保你在 Spring Boot 应用中启用了自动扫描功能,以便 Swagger 能够发现并处理包含注解的类。 5. 启动应用:运行你的 Spring Boot 应用,访问 `/docs` 或者根据之前配置的路径来查看 Swagger UI 页面。 相关问题-- 1. Spring Cloud GatewaySwagger 配置文件在哪里? 2. 如何在 Spring Cloud Gateway 中禁用 Swagger UI? 3. Spring Cloud Gateway 中如何设置 Swagger 的基本授权信息?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值