Zuul中整合Swagger2,实现对源服务API测试

Posted Lovnx

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Zuul中整合Swagger2,实现对源服务API测试相关的知识,希望对你有一定的参考价值。

前言

我们知道,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测试的主要内容,如果未能解决你的问题,请参考以下文章

Zuul中整合Swagger2,实现对源服务API测试

SpringCloud系列之四---Zuul网关整合Swaagger2管理API

Spring Cloud Zuul中使用Swagger汇总API接口文档

白话SpringCloud | 第十一章:路由网关(Zuul):利用swagger2聚合API文档

Spring Boot整合Swagger2

SpringBoot整合Swagger2