zuul中整合swagger2,实现对源服务api测试

Lovnx Lovnx     2022-12-08     397

关键词:

前言

我们知道,Swagger2整合到项目中,可以非常方便地进行接口测试,是前后端对接效率提高。现在,我们可以在Zuul中整合Swagger2,通过Zuul配置文件配置的映射路径,来生成源服务接口的测试Dashboard。
github项目源码地址

1、Zuul Server工程

pom.xml文件中引入依赖:

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>

Swagger2配置类:

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig 

    @Autowired
    ZuulProperties properties;

    @Primary
    @Bean
    public SwaggerResourcesProvider swaggerResourcesProvider() 
        return () -> 
            List<SwaggerResource> resources = new ArrayList<>();
            properties.getRoutes().values().stream()
                    .forEach(route ->
                            resources
                                    .add(createResource(route.getServiceId(), route.getServiceId(), "2.0")));
            return resources;
        ;
    

    private SwaggerResource createResource(String name, String location, String version) 
        SwaggerResource swaggerResource = new SwaggerResource();
        swaggerResource.setName(name);
        swaggerResource.setLocation("/" + location + "/v2/api-docs");
        swaggerResource.setSwaggerVersion(version);
        return swaggerResource;
    

Swagger静态文件重定向:

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
@EnableWebMvc
public class ApplicationExceptionAdapter extends WebMvcConfigurerAdapter 

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) 
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    

2、源服务

pom.xml文件中引入依赖:

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>

Swagger2配置类:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication
@EnableDiscoveryClient
@EnableSwagger2
public class ClientAApplication 

    public static void main(String[] args) 
        SpringApplication.run(ClientAApplication.class, args);
    

    @Bean
    public Docket swaggerPersonApi10() 
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.springcloud.sample.controller"))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(
                        new ApiInfoBuilder()
                        .version("1.0")
                        .title("Original Service API")
                        .description("Original Service API v1.0")
                        .build()
                );
    

Controller添加Swagger2的注解:

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(description = "测试源服务API接口")
@RestController
public class TestController 

    @ApiOperation(value = "加法", notes = "加法")
    @GetMapping("/add")
    public Integer add(Integer a, Integer b)
        return a + b;
    
    @ApiOperation(value = "减法", notes = "减法")
    @GetMapping("/sub")
    public Integer sub(Integer a, Integer b)
        return a - b;
    
    @ApiOperation(value = "乘法", notes = "乘法")
    @GetMapping("/mul")
    public Integer mul(Integer a, Integer b)
        return a * b;
    
    @ApiOperation(value = "除法", notes = "除法")
    @GetMapping("/div")
    public Integer div(Integer a, Integer b)
        return a / b;
    

3、启动项目,测试

zuul中整合swagger2,实现对源服务api测试

前言我们知道,Swagger2整合到项目中,可以非常方便地进行接口测试,是前后端对接效率提高。现在,我们可以在Zuul中整合Swagger2,通过Zuul配置文件配置的映射路径,来生成源服务接口的测试Dashboard。github... 查看详情

springcloud系列之四---zuul网关整合swaagger2管理api

...eka服务端,再创建多个微服务项目,每个微服务项目整合swagger2,形成接口文档,并且每个微服务需要注册到eureka注册中心。再创建zuul网关对所有的swagger进行管理,zuul网关也需要作为eureka客户端注册到注册中心。GitHub源码链接位... 查看详情

白话springcloud|第十一章:路由网关(zuul):利用swagger2聚合api文档

前言通过之前的两篇文章,可以简单的搭建一个路由网关了。而我们知道,现在都奉行前后端分离开发,前后端开发的沟通成本就增加了,所以一般上我们都是通过swagger进行api文档生成的。现在由于使用了统一路由网关了,都... 查看详情

16springcloud整合swagger2构建restful服务的apis

喜欢关注个人公众号:java乐园SpringCloud将服务注册到了Eureka上,可以从Eureka的UI界面中,看到有哪些服务已经注册到了EurekaServer上;但是如果想查看当前服务提供了哪些RESTful接口方法的话,就无法从EurekaServer获取了,而传统的方... 查看详情

16springcloud整合swagger2构建restful服务的apis

SpringCloud将服务注册到了Eureka上,可以从Eureka的UI界面中,看到有哪些服务已经注册到了EurekaServer上;但是如果想查看当前服务提供了哪些RESTful接口方法的话,就无法从EurekaServer获取了,而传统的方法是梳理一个接口文档来供开... 查看详情

springboot整合swagger2

一关于SwaggerSwagger能成为最受欢迎的RESTAPIs文档生成工具之一,有以下几个原因:Swagger可以生成一个具有互动性的API控制台,开发者可以用来快速学习和尝试API。Swagger可以生成客户端SDK代码用于各种不同的平台上的实现。Swagger... 查看详情

springboot整合swagger2搭建api在线文档

...能强大的在线API在线文档,目前它的版本为2.x,所以称为Swagger2。Swagger2提供了在线文档的查阅和测试功能。利用Swagger2很容易构建RESTful风格的API,在SpringBoot中集成Swagger2,步骤如下。1.引入依赖<!--Swagger2--><dependency><gr... 查看详情

一分钟完成springboot项目整合swagger2实现自动生成接口文档

...以维护、浪费开人员时间、文档难以与接口保持一致等。Swagger2的出现很好的解决了上述问题,可以实现接口文档实时在线生成,提供在线接口测试功能。唯一的弊端就是对接口程序有侵入,但本人认为还是利大于弊的。接下来... 查看详情

微服务架构中整合网关权限服务(代码片段)

...过微服务的权限系列和网关实现,都是孤立存在,本文将整合后端服务与网关、权限系统。安全权限部分的实现还讲解了基于前置验证的方式实现,但是由于与业务联系比较紧密,没有具体的示例。业务权限与业务联系非常密切... 查看详情

springsession和springsecurity整合

背景:我要做的系统前面放置zuul。使用自己公司提供的单点登录服务。后面的业务应用也是springboot支撑的rest服务。计划用户请求过来之后。通过自定义的filter在zuul实现单点登录。把用户的信息包括账户和权限存入session。zuul和... 查看详情

springboot整合swagger2框架

一:什么是SwaggerSwagger是一款通过我们添加的注解来对方法进行说明,来自动生成项目的在线api接口文档的web服务。二:添加Swagger2依赖<dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><ve 查看详情

springboot整合swagger2

SpringBoot整合Swagger2相信各位在公司写API文档数量应该不少,当然如果你还处在自己一个人开发前后台的年代,当我没说,如今为了前后台更好的对接,还是为了以后交接方便,都有要求写API文档。手写Api文档的几个痛点:文档需... 查看详情

springcloud微服务(05):zuul组件,实现路由网关控制

...2)聚合API接口,统一对外暴露,提高系统的安全性;3)实现请求统一的过滤, 查看详情

springboot之swagger2集成

一、Swagger2简单介绍  Swagger2,它可以轻松的整合到SpringBoot中,并与SpringMVC程序配合组织出强大RESTfulAPI文档。它既可以减少我们创建文档的工作量,同时说明内容又整合入实现代码中,让维护文档和修改代码整合为一体,可以... 查看详情

路由网关(zuul)(代码片段)

...,是实现请求校验,服务聚合等功能的基础.Zuul和Eureka进行整合,将Zuul自身注册为Eureka服务治理下的应用,同时从Eureka中获得其他微 查看详情

Hystrix 断路器实现在 Zuul API 网关级别或 REST API 服务级别

】Hystrix断路器实现在ZuulAPI网关级别或RESTAPI服务级别【英文标题】:HystrixCircuitBreakerImplementationbeatZuulAPIGatewayLeveloratRESTAPIServiceLevel【发布时间】:2019-03-1903:40:22【问题描述】:例如,我有两个RestApi服务正在运行https://my-app-one.com/... 查看详情

[idea]springboot整合swagger2实现crud

一:创建SpringBoot,在pom.xml文件中加入jar包  <dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.8.0</version></dependenc 查看详情

zuul修改请求头响应头(死磕)(代码片段)

...疯狂创客圈高并发社群博客前言由于SpingSecurity+SpringSession整合场景,涉及到修改Zuul请求头的问题。故梳理一个比较全面的Zuul修改请求头、响应头的文章。1SpingSecurity+SpringSession整合场景二本场景为统一网关+微服务场景:网关Sping... 查看详情