zuul网关集成swagger

zuul网关集成swaggerswagger2是一个API文档生成工具,在微服务的架构中,一般会使用zuul作为api网关,适合用来集成swagger生成所有微服务的接口文档。(springboot版本1.5.9)zuul服务添加依赖springfox-swagger2是用于生成接口文档的,必须要依赖springfox-swagger-ui负责提供ui查询界面,这里因为是在zuul集成,所以只需要z…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

     swagger2是一个API文档生成工具,在微服务的架构中,一般会使用zuul作为api网关,适合用来集成swagger生成所有微服务的接口文档。(springboot版本1.5.9)

 

zuul服务添加依赖

springfox-swagger2是用于生成接口文档的,必须要依赖

springfox-swagger-ui负责提供ui查询界面,这里因为是在zuul集成,所以只需要zuul依赖就可以了,其他的应用只负责提供接口文档的数据,不需要ui界面查询,所以无需依赖

       <!--生成接口文档-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>
        <!--提供ui界面-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
        </dependency>

zuul添加swagger配置

import org.springframework.cloud.netflix.zuul.filters.Route;
import org.springframework.cloud.netflix.zuul.filters.RouteLocator;
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;
import java.util.ArrayList;
import java.util.List;

//通过configuration注解自动注入配置文件
@Configuration
//开启swagger功能
@EnableSwagger2
//如果有多个配置文件,以这个为准
@Primary
//实现SwaggerResourcesProvider,配置swagger接口文档的数据源
public class SwaggerConfig  implements SwaggerResourcesProvider {

    //RouteLocator可以根据zuul配置的路由列表获取服务
    private final RouteLocator routeLocator;

    public SwaggerConfig(RouteLocator routeLocator) {
        this.routeLocator = routeLocator;
    }

    //这个方法用来添加swagger的数据源
    @Override
    public List<SwaggerResource> get() {
        List resources = new ArrayList();
        List<Route> routes = routeLocator.getRoutes();
        //通过RouteLocator获取路由配置,遍历获取所配置服务的接口文档,这样不需要手动添加,实现动态获取
        for (Route route: routes) {
            resources.add(swaggerResource(route.getId(), route.getFullPath().replace("**", "v2/api-docs"),"2.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;
    }

}

一般来说zuul服务不会额外提供接口,所以zuul服务本身不需要创建swagger文档,到这里就完成了与swagger的集成,下面是其他服务的集成

 

================================================================================================

添加依赖

是需要依赖springfox-swagger2即可

       <!--生成接口文档-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>

添加配置文件

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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.*;
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()
                //扫描com.au.sa包下文件
                .apis(RequestHandlerSelectors.basePackage("com.au.sa"))
                //扫描@Api注解的类
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                //扫描@ApiOperation注解的方法
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }

    //接口文档的描述
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("XXX模块")
                .description("XXX模块")
                .version("1.0")
                .build();
    }


}

创建接口类

接口类注意要在上面扫描的com.au.sa包下面,类要注入@Api,需要生成接口文档的接口需要加上@ApiOperation注解

@Api(tags = "customer服务接口")
@RestController
@RequestMapping("/customerService")
public class CustomerService extends MyCommonService {

    private static final Logger LOGGER = LoggerFactory.getLogger(CustomerService.class);
    @Autowired
    private ICustomer customerServer;

    @Override
    public IMybatisBaseCommon<?> getBaseCommonServer() {
        return this.customerServer;
    }


    @ApiOperation("查询列表(不分页)")
    @ApiImplicitParam(name = "params", value = "查询参数",dataType = "String")
    @GetMapping("/findCustomerList")
    public String findCustomerList(@RequestParam(required = false) String params){
        return this.findList(params);
    }

    @ApiOperation("分页查询")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "params",value = "查询参数",dataType = "String", required = true),
            @ApiImplicitParam(name = "pageIndex",value = "当前页码", dataType = "int"),
            @ApiImplicitParam(name = "pageRows",value = "分页大小", dataType = "int")})
    @GetMapping("/findCustomerPagination")
    public String findCustomerPagination(@RequestParam(required = false) String params,
                                         @RequestParam(required = false,defaultValue = "1") Integer pageIndex,
                                         @RequestParam(required = false,defaultValue = "10") Integer pageRows){
        return super.findPagination(params,pageIndex,pageRows);
    }


    @ApiOperation("根据主键ID获取对象")
    @ApiImplicitParam(name = "params", value = "json字符串格式,必须包含参数id",dataType = "String", required = true)
    @PostMapping("/findById")
    public String findById(@RequestParam String params) {
        return super.findById(params);
    }


    @ApiOperation("保存")
    @ApiImplicitParam(name = "params",value = "需要保存的对象,json字符串格式", dataType = "String", required = true)
    @PostMapping("/save")
    public String saveOrUpdate(@RequestParam String params) {
        return super.saveOrUpdate(params);
    }

    @ApiOperation("删除")
    @ApiImplicitParam(name = "params", value = "json字符串格式,必须包含参数id,多个id用逗号隔开", dataType = "String", required = true)
    @PostMapping("/delete")
    public String delete(@RequestParam String params) {
        return super.delete(params);
    }
}

 

验证

zuul服务起来之后就可以访问到swagger了,这里zuul因为是加了api前缀,所以访问的时候要加上/api,一般来说直接主机ip+端口号+/swagger-ui.html就可以访问了,下拉列表就是根据zuul的路由配置所拿到的服务。(这里还没有起其他服务,所以是500)

zuul网关集成swagger

接下来把其他服务启动一下,然后在界面选择对应的服务,起来之后就可以看到扫描出来的接口

zuul网关集成swagger

点击具体接口可以看到接口的详细说明,这些说明都是根据接口类中方法的注解生成的,具体注解属性对应的说明自行百度一下swagger的注解说明

zuul网关集成swagger

 

这里记录一下遇到的几个坑:

1.swagger2的获取文档的接口以及页面等静态资源都是依赖包中提供的,如果项目中对请求有拦截的话需要将swagger的相关接口添加到例外,否则将无法访问,springboot的可以使用corsconfig的方式添加排除,主要将下面几个前缀的添加到例外

whiteList.add("swagger-resources");
whiteList.add("configuration");
whiteList.add("v2/");
whiteList.add("swagger-ui.html");
whiteList.add("webjars");

2.其他服务类在配置swagger的时候,createRestApi()生成接口文档扫描时不要贪图方便直接扫描@Api或者@ApiOperation,还是按照上面的扫描对应的包下面的,否则会将swagger自身的接口也会一起扫描出来或者是扫描不到方法

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/170629.html原文链接:https://javaforall.net

(0)
上一篇 2022年8月15日 下午1:36
下一篇 2022年8月15日 下午10:16


相关推荐

  • yodgor mirzajonov_jacqueline novogratz

    yodgor mirzajonov_jacqueline novogratz1142.MaximalClique(25)题目:Acliqueisasubsetofverticesofanundirectedgraphsuchthateverytwodistinctverticesinthecliqueareadjacent.Amaximalcliqueisacliquethatcannotbee…

    2026年4月17日
    4
  • 敏捷项目管理的流程_敏捷开发项目管理方法

    敏捷项目管理的流程_敏捷开发项目管理方法引言:敏捷绝非某一种特定的开发方法,它只是一种应对快速变化的需求的一种软件开发能力。敏捷本身只包含了《敏捷软件开发宣言》和《敏捷软件的十二条原则》两份文档。敏捷的起源:敏捷开发以用户的需求进化为核心,采用迭代、循序渐进的方法进行软件开发。在敏捷开发中,软件项目在构建初期被切分成多个子项目,各个子项目的成果都经过测试,具备可视、可集成和可运行使用的特征。换言之,就是把一个大项目分为多个相互联系,但也可独立运行的小项目,并分别完成,在此过程中软件一直处于可使用状态。目前很多互联网公司都在搞或者想

    2025年6月22日
    5
  • C语言结构体指针_C语言函数返回结构体指针

    C语言结构体指针_C语言函数返回结构体指针文章目录结构体概述结构体指针结构体概述问题定义:有时需要将不同类型的数据组合成一个有机的整体,以便于使用,就类似于sql中的存储一样,随着语言层次的增高封装性是越来越大的。如:intnum;charname[20];charsex;intage;charaddr[30];定义结构的一般形式为:struct结构名{成员列表};成员列表由若干个成员构成,每个成员…

    2025年8月14日
    6
  • 百度面试面经[通俗易懂]

    百度面试面经[通俗易懂]作者:球球球offer链接:https://www.nowcoder.com/discuss/230987来源:牛客网看到有帖子说百度的面经少,我发一下面经攒人品,许愿之后的面试都顺利!一共面了两

    2022年7月4日
    31
  • c winform 浏览器调用chrome内核

    c winform 浏览器调用chrome内核privatevoidF Load objectsender EventArgse varsettings newCefSettin CefSharp Cef Initialize settings browser newChromiumW https aaa com auth account chinav

    2026年3月19日
    2
  • 虚拟服务器 emule,emule服务器

    虚拟服务器 emule,emule服务器emule服务器内容精选换一换生产站点服务器为SUSE操作系统,对该云服务器创建容灾演练后,发现容灾演练云服务器的网卡名称与生产站点服务器的网卡名称不一致。示例:操作系统为NovellSUSELinuxEnterpriseServer12SP364bit的生产站点服务器,挂载有5张网卡。登录生产站点服务器,查询网卡名称为eth0~eth4,如图1所示。创远程桌面协议(Remote…

    2022年6月15日
    30

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号