zuul网关整合swagger

本文介绍了如何在Spring Cloud微服务环境中将Zuul与Swagger进行整合,实现API网关的文档化管理。通过在服务启动类上添加@EnableSwagger2Doc注解,并配置Swagger资源,可以方便地访问各服务的Swagger接口文档。同时,文章提到了如果系统集成了权限管理,Swagger也需要相应登录机制以确保安全。
摘要由CSDN通过智能技术生成

zuul整合swagger网关

maven依赖

 <dependency>
            <groupId>com.spring4all</groupId>
            <artifactId>swagger-spring-boot-starter</artifactId>
            <version>1.7.0.RELEASE</version>
        </dependency>

服务配置

1.首先例如你有服务a,服务b,首先在服务的启动类上都要添加swagger的注解 @EnableSwagger2Doc

@SpringBootApplication
@EnableEurekaClient
@EnableSwagger2Doc
@MapperScan("com.ams.manager.application.impl.mapper")
public class AppApplication {
    public static void main(String[] args) {
        SpringApplication.run(AppApplication.class);
    }
}

这样你就可以访问swagger的页面了。
在这里插入图片描述

application.yml配置

注意base-package:的配置更具自己的实际包来

swagger:
  base-package: com.ams.manager.application.impl.service
  title: SpringCloud2.x构建微服务家庭专区项目-系统服务接口
  description: 该项目“基于SpringCloud2.x构建微服务项目”吴坚版权所有,未经过允许的情况下,私自分享视频和源码属于违法行为。
  version: 1.1
  terms-of-service-url: www.wujian.com
  contact:
    name: 全网最帅架构师-吴坚
    email: 1239133508@qq.com
    url: www.wujian.com
  enabled: true

整合zuul

1. 网关启动类配置

网关的启动类上需要加上@EnableSwagger2Doc注解

2.添加swagger配置

在这里插入图片描述

注意修改资源名称位服务名称

package com.ams.swagger.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
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
public class SwaggerConfig {
    // 添加文档来源
    @Component
    @Primary
    class DocumentationConfig implements SwaggerResourcesProvider {
        @Override
        public List<SwaggerResource> get() {
            List resources = new ArrayList<>();
            //配置路由映射项目路径,其他的例如会员和订单服务的Swagger2文档的路径映射配置
            resources.add(swaggerResource("系统服务", "/ams-manager-system/v2/api-docs", "2.0"));
            resources.add(swaggerResource("应用服务", "/ams-manager-application/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;
        }
    }
}

注意

如果你的系统是整合了权限系统,那么这时候你的swagger需要进行登录,这样也是比较合理的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值