首页 > 其他分享 >SpringCloudAlibaba Gateway聚合knife4j接口文档

SpringCloudAlibaba Gateway聚合knife4j接口文档

时间:2023-08-02 16:56:09浏览次数:37  
标签:knife4j swaggerResource springframework org import swagger SpringCloudAlibaba Ga

实现效果:

Gateway服务可以查看多个服务的接口文档;

 

knife4j依赖:

        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-boot-starter</artifactId>
            <version>3.0.3</version>
        </dependency>


自定义实现SwaggerResourcesProvider,在网关中查询路由信息,生成对应的资源信息:

import lombok.RequiredArgsConstructor;
import org.springframework.cloud.gateway.config.GatewayProperties;
import org.springframework.cloud.gateway.route.RouteLocator;
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 java.util.ArrayList;
import java.util.List;

/**
 * @ClassName SwaggerResourcesProviderImpl
 * @Description 自定义实现SwaggerResourcesProvider,在网关中查询路由信息,生成对应的资源信息
 * @Author XinHai.Ma
 * @Date 2023/8/2 15:46
 * @Version 1.0
 */
@Primary // 主配置
@Component("swaggerResourcesProviderImpl")
@RequiredArgsConstructor
public class SwaggerResourcesProviderImpl implements SwaggerResourcesProvider {

    /**
     * swagger2默认的url后缀
     */
    private static final String SWAGGER2_URL = "/v2/api-docs";

    /**
     * 路由定位器
     */
    private final RouteLocator routeLocator;

    /**
     * 网关配置文件
     */
    private final GatewayProperties gatewayProperties;

    @Override
    public List<SwaggerResource> get() {
        List<SwaggerResource> resources = new ArrayList<>();
        List<String> routes = new ArrayList<>();
        //获取网关中配置的route
        routeLocator.getRoutes().subscribe(route -> routes.add(route.getId()));
        gatewayProperties.getRoutes().stream().filter(routeDefinition -> routes.contains(routeDefinition.getId()))
                .forEach(routeDefinition -> routeDefinition.getPredicates().stream()
                        .filter(predicateDefinition -> "Path".equalsIgnoreCase(predicateDefinition.getName()))
                        .forEach(predicateDefinition -> resources.add(
                                swaggerResource(
                                        routeDefinition.getId(),
                                        predicateDefinition
                                                .getArgs()
                                                .get(NameUtils.GENERATED_NAME_PREFIX + "0")
                                                .replace("/**", SWAGGER2_URL)
                                )
                        )));
        return resources;
    }

    private SwaggerResource swaggerResource(String name, String location) {
        SwaggerResource swaggerResource = new SwaggerResource();
        swaggerResource.setName(name);
        swaggerResource.setLocation(location);
        swaggerResource.setSwaggerVersion("3.0");
        return swaggerResource;
    }

}

 

添加一个/swagger-resources访问接口,覆盖默认:

import com.maxinhai.mesgateway.config.SwaggerResourcesProviderImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.swagger.web.SwaggerResource;

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

/**
 * @ClassName SwaggerResourceController
 * @Description 添加一个/swagger-resources访问接口,覆盖默认
 * @Author XinHai.Ma
 * @Date 2023/8/2 15:48
 * @Version 1.0
 */
@RestController
@RequiredArgsConstructor
public class SwaggerResourceController {

    @Resource(name = "swaggerResourcesProviderImpl")
    private SwaggerResourcesProviderImpl swaggerResourceProvider;

    @RequestMapping(value = "/swagger-resources")
    public ResponseEntity<List<SwaggerResource>> swaggerResources() {
        return new ResponseEntity<>(swaggerResourceProvider.get(), HttpStatus.OK);
    }

}

 

标签:knife4j,swaggerResource,springframework,org,import,swagger,SpringCloudAlibaba,Ga
From: https://www.cnblogs.com/mxh-java/p/17601135.html

相关文章

  • SpringCloud Gateway 在微服务架构下的最佳实践
    作者:徐靖峰(岛风)前言本文整理自云原生技术实践营广州站Meetup的分享,其中的经验来自于我们团队开发的阿里云CSB2.0这款产品,其基于开源SpringCloudGateway开发,在完全兼容开源用法的前提下,做了诸多企业级的改造,涉及功能特性、稳定性、安全、性能等方面。为什么需要微服务网......
  • 最全springcloudAlibaba视频笔记-第三章Nacos Config服务配置中心
    NacosConfig服务配置中心课程视频:https://www.bilibili.com/video/BV1VW4y1o7n5本课程使用的是目前最新版本2022.0.0.0-RC2。基于SpringBoot3.0与JDK20的开发环境。集群中每一台主机的配置文件都是相同的,对配置文件的更新维护就成为了一个棘手的问题。此时就出现了配置中心......
  • Centos7如何配置IPADDR,NETMASK,GATEWAY?
    1、获取IPADDR、NETMASK:[root@192network-scripts]#ifconfigens33:flags=4163<UP,BROADCAST,RUNNING,MULTICAST>mtu1500inet192.168.85.139netmask255.255.255.0broadcast192.168.85.255inet6fe80::11df:c601:5b38:ca41prefixlen64s......
  • springcloudalibaba 从入门到精通进阶学习路线?深入细化讲解
    springcloudalibaba从入门到精通进阶学习路线?深入细化讲解下面是深入细化的SpringCloudAlibaba学习路线:1.学习SpringBoot和SpringCloud基础知识:在开始学习SpringCloudAlibaba之前,你需要先掌握SpringBoot和SpringCloud的基础知识。学习SpringBoot的核心概念、注解、配......
  • INFINI Labs 产品更新 | Easysearch 新增分词插件、Gateway 支持邮件发送等功能
    INFINILabs产品又更新啦~,本次更新概要如下:Easysearch新增了分词插件、优化了生命周期管理功能等;Gateway新增smtp过滤器来支持邮件的发送,支持自动跳过因为异常关闭而损坏的磁盘队列文件等;Console新增熔断器监控指标、新增矩形树图(Treemap)、优化了探针Agent指标采集和集......
  • Kong API Gateway 配置文件详解
    一、前言Kong配置文件是Kong服务的核心文件,它配置了Kong以怎样的方式运行,并且依赖于这个配置生成Nginx的配置文件,本文通过解读Kong配置文件,以了解Kong的运行和配置。在成功安装Kong以后,会有一个名为kong.conf.default默认的配置文件示例,如果是通过包管理器安装的,通常位于/etc/k......
  • swagger文档和 knife4j 文档
    老版本的swagger-bootstrap-ui,可以显示非RestController,可以测试html页面显示,可以和springfox-swagger-ui配合显示<!--swagger--><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><vers......
  • 网关流控规则GatewayFlowRule的核心属性
    resourceMode:规则是针对APIGateway的route(RESOURCE_MODE_ROUTE_ID)还是用户在Sentinel中定义的API分组(RESOURCE_MODE_CUSTOM_API_NAME),默认是route。resource:资源名称,可以是网关中的route名称或者用户自定义的API分组名称。grade:限流指标维度,同限流规则的grade字段。count:限流阀......
  • Rslinx 通过Gateway连接其他PC上的Rslinx Classic Gateway访问现场PLC
    1.在其中一台PC上开启Gateway2.添加IP访问限制3.在客户端上配置驱动选择RemoteDevicesviaLinxGateway4.设置好名称后点击Browse浏览网络中的Gateway5.配置完成在左侧树形菜单可以访问到在logix5000也可以通过这个路劲访问  使用此方式可以大幅降低PLC的......
  • Spring Cloud Gateway 设置全局接口访问日志
    SpringCloudGateway设置全局接口访问日志虽然网关只做转发,但是对于每个转发的请求,我们都希望能够在日志中打印出请求的信息,网上版本很多,踩了很多坑,目前没找到完美的解决方案,最后我这个应该是大成版。希望对大家有用。先贴代码,再说遇到什么坑吧。/***@authorchenzhangx*@d......