【Spring Cloud Gateway】⑥SpringBoot3.x集成SpringDoc指南

这篇具有很好参考价值的文章主要介绍了【Spring Cloud Gateway】⑥SpringBoot3.x集成SpringDoc指南。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

背景

Spring Cloud Gateway使用Netty作为嵌入式服务器,并基于响应式Spring WebFlux。做为微服务网关,多个微服务把API挂在Gateway上,如果查看某个APISwagger还要去各个子微服务中去查看,就很不方便,如果能在Gateway上直接查看各个微服务的API文档,会方便很多,本文以截至目前最新的版本为示例,讲解如何在Spring Cloud Gateway中集成SpringDoc

SpringBoot 3.x需要SpringDoc 2.x

本地开发环境介绍

开发依赖 版本
Spring Boot 3.0.6
Spring Cloud 2022.0.2
Spring Cloud Alibaba 2022.0.0.0-RC2
Spring Doc 2.1.0
JDK 20

pom.xml主要依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-gateway</artifactId>
    </dependency>
    <!--引⼊webflux-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>

    <!-- webflux+SpringDoc -->
    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-starter-common</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-starter-webflux-ui</artifactId>
    </dependency>
</dependencies>

application.yml

spring:
  application:
    name: demo-gateway
  cloud:
    gateway:
      api-prefix: /
      #路由配置
      routes:
        - id: wen3-framework-apidoc-springdoc-demo
          uri: http://localhost:7001
          predicates:
            - Path=/demo7001/**
          filters:
            - RewritePath=/demo7001/(?<path>.*), /$\{path}
        - id: openapi
          uri: http://localhost:${server.port}
          predicates:
            - Path=/v3/api-docs/**
          filters:
            - RewritePath=/v3/api-docs/(?<path>.*), /$\{path}/api-docs
  
######## swagger configuration ###############
springdoc:
  swagger-ui:
    urls:
      - name: demo7001
        url: /v3/api-docs/demo7001
  • demo7001是另一个SpringDoc演示服务,可以通过/api-docs获取swagger JSON数据即可
  • 如果有多个服务,就需要添加多个routes
  • 原理是通过调用微服务本身文档接口获取JSON数据,然后在网关的swagger-ui页面上展示

效果预览

浏览器访问http://localhost:8081/swagger-ui.html
【Spring Cloud Gateway】⑥SpringBoot3.x集成SpringDoc指南,Spring Boot,SpringBoot3,SpringDoc,Gateway,Swagger,SpringBoot

动态生成swagger文档分组

package com.wen3.demo.gateway.springdoc;

import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.properties.AbstractSwaggerUiConfigProperties;
import org.springdoc.core.properties.SwaggerUiConfigProperties;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.HashSet;

@Component
@Slf4j
public class SwaggerUiConfigPropertiesPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if(SwaggerUiConfigProperties.class.isAssignableFrom(bean.getClass())) {
            SwaggerUiConfigProperties obj = (SwaggerUiConfigProperties) bean;
            if(CollectionUtils.isEmpty(obj.getUrls())) {
                obj.setUrls(new HashSet<>());
            }
            obj.getUrls().add(new AbstractSwaggerUiConfigProperties.SwaggerUrl("demo7001", "/v3/api-docs/demo7001", "demo7001xx"));
        }
        return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
    }
}
  • 可以根据需要改造这个功能,可以在从routes配置中解析各个微服务,然后添加每个微服务的swagger文档
  • 也可以从数据库或其它地方获取需要开启的swagger文档

效果预览

【Spring Cloud Gateway】⑥SpringBoot3.x集成SpringDoc指南,Spring Boot,SpringBoot3,SpringDoc,Gateway,Swagger,SpringBoot文章来源地址https://www.toymoban.com/news/detail-630362.html

在线文档

  • 官网: https://springdoc.org/#getting-started

到了这里,关于【Spring Cloud Gateway】⑥SpringBoot3.x集成SpringDoc指南的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Spring Cloud Gateway集成Nacos实现负载均衡

    💡Nacas可以用于实现Spring Cloud Gateway中网关动态路由功能,也可以基于Nacos来实现对后端服务的负载均衡,前者利用Nacos配置中心功能,后者利用Nacos服务注册功能。 接下来我们来看下Gateway集成Nacos实现负载均衡的架构图 一. 环境准备 1. 版本环境 Jdk: java.version1.8/java.version Spr

    2024年02月10日
    浏览(36)
  • Spring Cloud Gateway集成sentinel进行网关限流

    本文使用版本如下:

    2024年02月09日
    浏览(38)
  • Spring Cloud Gateway集成Nacos作为注册中心和配置中心

    本篇文章将介绍Spring Cloud Alibaba体系下Spring Cloud Gateway的搭建,服务注册中心和分布式配置中心使用Nacos,后续将会持续更新,介绍集成Sentinel,如何做日志链路追踪,如何做全链路灰度发布设计,以及Spring Cloud Gateway的扩展等。 ​ Spring Boot,Spring Cloud,Discovery,Config等基础依

    2024年02月11日
    浏览(32)
  • Springboot3.0整合swagger,废弃Springfox改用Springdoc

    Automated JSON API documentation for API\\\'s built with Spring 官网地址:springfox.io springdoc-openapi java library helps to automate the generation of API documentation using spring boot projects. 官网地址:https://springdoc.org/v2/ 注意 :使用的是V2版本,这个版本支持springboot3.0 之前springboot3.0之前我用的都是Springfox来集

    2023年04月09日
    浏览(33)
  • 【SpringBoot3】Spring Boot 3.0 集成 Redis 缓存

    Redis缓存是一个开源的使用ANSIC语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。它主要用于作为数据库、缓存和消息中间件,以快速读写和丰富的数据结构支持而著称。 在应用程序和数据库之间,Redis缓存作为一个中间层起着关键

    2024年02月21日
    浏览(40)
  • Spring Cloud Gateway集成Sentinel 1.8.6及Sentinel Dashboard

    一、安装sentinel 1.下载地址:sentinel v1.8.6 2.启动sentinel dashboard,执行以下命令: java -Dcsp.sentinel.log.dir=D:xxxsentinellogs -Dserver.port=9217 -Dcsp.sentinel.dashboard.server=localhost:9217 -Dcsp.sentinel.heartbeat.client.ip=localhost -Dproject.name=sentinel-dashboard -Dsentinel.dashboard.auth.username=sentinel -Dsentinel.dashboar

    2024年02月11日
    浏览(39)
  • Spring Cloud Gateway集成Actuator的安全漏洞和解决方案

    Spring Cloud Gateway是一个基于Spring Boot2.0和Spring WebFlux的API网关,它可以将请求转发到多个微服务并对请求进行路由、过滤和修改。Spring Cloud Gateway集成Actuator后可以提供更多的监控和管理功能,但是也可能导致安全漏洞。 最近线上环境出现一起安全事件,就是由于Spring Cloud Gat

    2024年02月09日
    浏览(34)
  • 从零开始的Spring Cloud Gateway指南:构建强大微服务架构

    微服务架构的兴起已经改变了软件开发的面貌,使得开发者能够更灵活地构建、部署和维护应用程序。而在这个微服务的时代,强大而灵活的网关是确保微服务之间通信顺畅的关键之一。在本文中,我们将深入研究Spring Cloud Gateway,一款开源的、基于Spring Framework的微服务网关

    2024年02月02日
    浏览(39)
  • Spring Cloud Gateway集成Swagger实现微服务接口文档统一管理及登录访问

    本文将介绍如何在 Spring Cloud 微服务中使用 Swagger 网关来统一管理所有微服务的接口文档,并通过 Spring Security 实现登录后才能访问 Swagger 文档,以确保接口数据的安全访问。 在开始之前,需要假设你已经完成了 Spring Cloud Gateway 的相关配置,并且已经了解了基本的网关配置知

    2024年02月05日
    浏览(33)
  • springboot整合spring cloud gateway搭建网关服务

    spring cloud netflix zuul、spring cloud gateway是最常见的微服务网关,通过网关,我们可以在请求到达后端指定服务之前/后端服务处理完业务响应数据之后对响应进行对请求/响应进行处理。 比如常见的参数校验、接口鉴权等等,在后端服务的拦截器和过滤器能做的事在网关都可以做

    2024年02月07日
    浏览(38)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包