SpringCloud zull 整合Swagger

本文详细介绍如何在SpringCloud项目中使用Zull网关整合Swagger,实现API文档的统一管理和展示。通过添加依赖、配置Swagger2及启动类注解,完成各子模块API文档的自动整合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SpringCloud zull 整合Swagger

  1. 添加依赖

    <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>2.7.0</version>
        </dependency>
        <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>2.7.0</version>
        </dependency>
    
  2. 创建配置类

    package com.exchange.user.utils;
    
    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.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    import springfox.documentation.swagger.web.UiConfiguration;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    
    @Configuration
    public class Swagger2Config {
    
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.exchange.user.controller"))
                .paths(PathSelectors.any())
                .build();
    }
    
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("用户模块")
                .description("endless")
                .termsOfServiceUrl("http://www.baidu.com/")
                .contact("xydeveloper@126.com")
                .version("1.0")
                .build();
    }
    
    @Bean
    protected UiConfiguration uiConfig() {
        return new UiConfiguration(null, "list", "alpha", "schema",
                UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS, false, true, 60000L);
    }
    
    

}

```
  1. 启动类加注解

    package com.exchange.user;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.cache.annotation.EnableCaching;
    import org.springframework.cloud.client.SpringCloudApplication;
    import org.springframework.scheduling.annotation.EnableScheduling;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    import tk.mybatis.spring.annotation.MapperScan;
    
    @EnableCaching
    @EnableSwagger2
    @EnableScheduling
    @SpringCloudApplication
    @MapperScan("com.exchange.user.mapper")
    public class UserApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(UserApplication.class);
        }
    
    }
    
    
    1. 网关添加Swagger整合各个子模块的总配置文件
    package com.exchange.gateway.config;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.cloud.client.discovery.DiscoveryClient;
    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 SwaggerUniteConfig implements SwaggerResourcesProvider {
    
    @Value("${PREFIX}")
    private String PREFIX;
    
    @Value("${SUFFIX}")
    private String SUFFIX;
    
    @Value("${VERSION}")
    private String VERSION;
    
    @Value("${REMOVE_SERVER_NAME}")
    private String REMOVE_SERVER_NAME;
    
    @Value("${SERVER_PREFIX}")
    private String SERVER_PREFIX;
    
    @Autowired
    private DiscoveryClient discoveryClient;
    
    @Override
    public List<SwaggerResource> get() {
    
        List listSwaggerUi = new ArrayList<>();
    
        discoveryClient.getServices().forEach(serverName -> {
    
            SwaggerResource swaggerResource = new SwaggerResource();
            swaggerResource.setSwaggerVersion(VERSION);
    
            if (!(SERVER_PREFIX + REMOVE_SERVER_NAME).equals(serverName)) {
                serverName = serverName.replace(SERVER_PREFIX, "");
                swaggerResource.setName(serverName);
                swaggerResource.setLocation(PREFIX + serverName + SUFFIX);
                listSwaggerUi.add(swaggerResource);
            }
    
        });
    
        return listSwaggerUi;
    
    }
    
    }
    
    
    1. 网关appcation常量
    spring:
      application:
        name: exchange-gateway
    eureka:
      client:
        service-url:
          defaultZone: http://127.0.0.1:9000/eureka/
        registry-fetch-interval-seconds: 5
      instance:
        prefer-ip-address: true
        ip-address: 127.0.0.1
    zuul:
      prefix: /exchange
      routes:
        exchange-user: /user/**
        exchange-manage: /manage/**
        exchange-trade-coin2coin: /coin2coin/**
        exchange-sto: /sto/**
        exchange-storage: /storage/**
        exchange-system: /system/**
        exchange-sto-manage: /sto-manage/**
        exchange-agent: /agent/**
        exchange-o2o: /o2o/**
        exchange-user-app: /user-app/**
        exchange-sto-app: /sto-app/**
    hystrix:
      command:
        default:
          execution:
            isolation:
              thread:
                timeoutInMilliseconds: 70000
    ribbon:
      connetTimeout: 60000
      readTimeout: 60000
    
    PREFIX: /exchange/
    SUFFIX: /v2/api-docs
    VERSION: 1.0
    REMOVE_SERVER_NAME: gateway
    SERVER_PREFIX: exchange-
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值