Spring Cloud Zuul整合Swagger2

2023-05-16

Spring Boot 专栏:https://blog.csdn.net/dkbnull/category_9278145.html
Spring Cloud 专栏:https://blog.csdn.net/dkbnull/category_9287932.html
GitHub:https://github.com/dkbnull/SpringBootDemo
Gitee:https://gitee.com/dkbnull/SpringBootDemo

Spring Boot整合Swagger2:https://blog.csdn.net/dkbnull/article/details/88380987

0. 开发环境

  • IDE:IntelliJ IDEA 2019.1.2
  • JDK:1.8.0_211
  • Spring Boot:2.0.9.RELEASE
  • Spring Cloud:Finchley.RELEASE

1. 新建服务提供者

这里我们直接改造spring-boot-providerspring-boot-provider-v2,两个服务均做如下改造

1.1 引入依赖

        <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>

1.2 新建Swagger2配置类

package cn.wbnull.springbootprovider.swagger;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.wbnull.springbootprovider.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Spring Cloud Demo Api")
                .contact(new Contact("dukunbiao(null)", "https://blog.csdn.net/dkbnull", ""))
                .version("1.0.0")
                .description("Spring Cloud Demo")
                .build();
    }
}

1.3 Restful接口增加注解

package cn.wbnull.springbootprovider.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@Scope("prototype")
@Api(tags = "测试接口")
public class GatewayController {

    @GetMapping(value = "/gateway")
    @ApiOperation("测试接口")
    public String gateway() throws Exception {
        return "hello world,this is spring-boot-provider";
    }

    @PostMapping(value = "/user")
    @ApiOperation("用户测试接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "用户名", required = true)
    })
    public String user(@RequestParam(value = "name") String name) throws Exception {
        return "hello world,this is spring-boot-provider. name is " + name;
    }

    @PostMapping(value = "/users")
    @ApiOperation("批量用户测试接口")
    public Map<String, String> users(@RequestBody Map<String, String> request) throws Exception {
        request.put("hello world", "spring-boot-provider");

        return request;
    }
}

1.4 测试

依次启动spring-cloud-eureka、spring-boot-provider、spring-boot-provider-v2,浏览器访问http://127.0.0.1:8081/springbootprovider/swagger-ui.html、http://127.0.0.1:8083/springbootprovider/swagger-ui.html
在这里插入图片描述

2. 新建Zuul服务

这里我们直接改造spring-cloud-zuul

2.1 引入依赖

        <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>

2.2 新建Swagger2配置类

package cn.wbnull.springcloudzuul.swagger;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Spring Cloud Demo Api")
                .contact(new Contact("dukunbiao(null)", "https://blog.csdn.net/dkbnull", ""))
                .version("1.0.0")
                .description("Spring Cloud Demo")
                .build();
    }
}

2.3 新建Swagger2资源文档配置类

package cn.wbnull.springcloudzuul.swagger;

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;

@Component
@Primary
public class DocumentationConfig implements SwaggerResourcesProvider {

    @Override
    public List<SwaggerResource> get() {
        List<SwaggerResource> resources = new ArrayList<>();
        resources.add(swaggerResource("spring-boot-provider", "/spring-boot-provider/springbootprovider/v2/api-docs", "1.0.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 修改GlobalFilter服务过滤类

之前创建的服务过滤类会校验token,我们把swagger类请求过滤掉

package cn.wbnull.springcloudzuul.filter;

@Component
public class GlobalFilter extends ZuulFilter {

	//code

    @Override
    public Object run() throws ZuulException {
        RequestContext context = RequestContext.getCurrentContext();
        HttpServletRequest servletRequest = context.getRequest();

        if (servletRequest.getRequestURI().contains("v2/api-docs")) {
            return null;
        }

        Object token = servletRequest.getHeader("token");
        if (token == null) {
            context.setSendZuulResponse(false);
            context.setResponseStatusCode(401);
            try {
                context.getResponse().getWriter().write("error: token is null");
            } catch (IOException e) {
            }
        }

        return null;
    }
}

2.5 测试

依次启动spring-cloud-eureka、spring-boot-provider、spring-boot-provider-v2、spring-cloud-zuul,浏览器访问http://127.0.0.1:8091/springcloudzuul/swagger-ui.html
在这里插入图片描述



GitHub:https://github.com/dkbnull/SpringCloudDemo



本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring Cloud Zuul整合Swagger2 的相关文章

随机推荐

  • android zram 命令,Android 内存管理

    概述 虚拟内存 2 1 分页 2 2 内存映射 内存不足时的处理 3 1 kswapd 3 2 LMK 虚拟机 4 1 堆空间划分 4 2 回收算法 在看这篇文章之前 xff0c 需要Linux内存管理基础 xff0c 推荐Linux 内存
  • C语言--结构体指针需要malloc

    如果定义一个结构体类型的普通变量 xff0c 可以不malloc动态申请内存 xff0c CPU会为这个结构体变量分配内存 如果定义的是一个结构体的指针 xff0c CPU会为这个指针开辟内存 xff0c 但是此时这个大小是4 xff08
  • Git分支创建命令

    Git分支创建命令 万次阅读 多人点赞 span class token number 2019 span span class token operator span span class token number 06 span spa
  • 「LSTM 之父」亲笔万字长文,只为向世人证明:深度学习不是在母语为英语的地方被发明的

    导语 xff1a 最重要的并不是谁发明了某项技术 xff0c 而是技术本身对于人类文明发展所具有的无上价值 xff01 雷锋网 AI 科技评论按 xff1a 毫无疑问 xff0c 深度学习是影响当今世界科技发展的最重要的技术之一 2018
  • 多任务学习-Multitask Learning概述

    2020 02 22 09 59 48 1 单任务学习VS多任务学习 单任务学习 xff1a 一次只学习一个任务 xff08 task xff09 xff0c 大部分的机器学习任务都属于单任务学习 多任务学习 xff1a 把多个相关 xff
  • 任奎:人工智能算法安全浅析——深度学习中的对抗攻击与防御

    2020 05 19 19 52 46 任奎 随着计算机产业发展带来的计算性能与处理能力的大幅提高 xff0c 人工智能在音视频识别 自然语言处理和博弈论等领域得到了广泛应用 在此背景下 xff0c 确保人工智能的核心 深度学习算法具有可靠
  • AI构图:AI摄影的新未来

    编辑导语 xff1a AI与摄影相结合已经不是新鲜事 xff0c 新鲜的是二者结合的方式 对于很多小伙伴来说 xff0c 拍照时最大的困扰就是不知道该如何构图 xff0c 如今AI构图已经出现了 xff0c 手机会在你拍照的时候针对情况做出
  • 浅谈民用无人机的行业化应用场景

    随着技术的发展 xff0c 人类在力学 材料学 电子技术 自动控制 计算机等方面陆续取得进步 xff0c 研制出了迷你无人机 xff0c 机型更加小巧 性能更加稳定 xff0c 同时无人机更加进步 智能化的技能 xff0c 催发了民用无人机
  • Spring Boot 整合Logback记录日志

    Spring Boot 专栏 xff1a https blog csdn net dkbnull category 9278145 html Spring Cloud 专栏 xff1a https blog csdn net dkbnull
  • 对于AI 既有期待也要思考

    近日 xff0c 在第五届世界智能大会新闻发布会上 xff0c 包括发改委 科技部 工信部等在内的多部门相关负责人表示 xff0c 将更加注重我国人工智能行业顶层设计 xff0c 全力推动人工智能产业发展 关于AI xff0c 笔者有两个问
  • 神经网络中Batch和Epoch之间的区别是什么?

    神经网络中Batch和Epoch之间的区别是什么 xff1f https mp weixin qq com s FFF6RSudAas7j2vHqP5j2Q 随机梯度下降法是一种具有大量超参数的学习算法 通常会使初学者感到困惑的两个超参数
  • 大脑升维:人工智能浪潮下的适者生存之道

    https www toutiao com a6675488003453878792 的媒介 xff0c 大家的主要精力放在思想输出的训练上 xff0c 而不是知识信息的输入上 xff0c 这样的大脑修炼速度在中维知识层面上是 慢 的 xf
  • 人工智能技术映射出来的16个行业66个应用场景!

    近期 xff0c 国际数据公司 xff08 IDC xff09 与百度AI产业研究中心 xff08 BACC xff09 联合发布了 百度大脑领导力白皮书 xff0c 白皮书中追踪了16个行业的66个应用场景 xff0c 并将人工智能在企业
  • 图像识别AI遇上对抗性图像变“瞎子”,准确率猛降90%

    https www toutiao com a6715945584722706956 在视觉方面 xff0c AI和人类的差距有多大 xff1f 来自UC Berkeley等高校的研究人员创建了一个包含7500个 自然对抗实例 的数据集 x
  • linux线程io调度策略,Linux IO调度算法

    IO调度器的总体目标是希望让磁头能够总是往一个方向移动 移动到底了再往反方向走 这恰恰就是现实生活中的电梯模型 所以IO调度器也被叫做电梯 elevator 而相应的算法也就被叫做电梯算法 而Linux中IO调度的电梯算法有好几种 一个叫做
  • 无人机入门(一)位置与电机速度控制

    无人机入门 xff08 一 xff09 作为无人机入门PX4可能过于庞大 xff0c 目前还没有支持ros noetic xff0c 而且PX4的底层框架我看也用到了ethz实验室的rotors xff0c 因此选用该模型作为入门首选 因为
  • de1 soc linux开发实例,DE1_SoC的第一次接触

    Altera 2013 教师会议上友晶科技在大陆地区正式推出了DE1 SoC xff0c 有消息称明年的亚洲创新大赛将采用SoC FPGA作为核心器件 那这也将预示着SoC FPGA正式进入了校园 作为一片性能适中的开发板 xff0c 有着
  • #include中尖括号和双引号的区别

    include lt gt 和 include 的区别 一 引用的头文件不同 include lt gt 引用的是编译器的类库路径里面的头文件 include 引用的是你程序目录的相对路径中的头文件 二 用法不同 include lt gt
  • 3分钟弄懂python函数传参是否改变外部变量的值

    这个具体要看传入数据的类型 xff0c python中有六大基本类型 xff1a 数字 xff0c str xff0c list xff0c turple xff0c dict xff0c set 其中数字 xff0c str xff0c
  • Spring Cloud Zuul整合Swagger2

    Spring Boot 专栏 xff1a https blog csdn net dkbnull category 9278145 html Spring Cloud 专栏 xff1a https blog csdn net dkbnull