1. 程式人生 > >Zuul中整合Swagger2,實現對源服務API測試

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、啟動專案,測試

這裡寫圖片描述

這裡寫圖片描述