首先感谢一位博主的分享https://www.cnblogs.com/xiaohouzai/p/8886671.html

话不多说直接上图和代码

首先我们要有一个springcloud分布式项目

我就简单的一个eureka注册中心然后就是一个user用户然后当然就是zuul网关拉。

eureka的配置我就不多说了,我直接贴zuul网关和swagger的相关配置

zuul中的配置

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.2.RELEASE</version>
</dependency>
<--swagger2依赖-->
<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>

zuul 的application.yml

#网关配置
zuul:
LogFilter: #自定义过滤器的名称
pre: #自定义过滤器类型
disable: true # 是否禁用false
prefix: /api/ #网关前缀如:http://localhost;8080/api/其他服务名
routes: #路由规则
user-server: #用户的服务名
path: /user/** #你定义的zuul网关名

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.UiConfiguration;
import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration
@EnableSwagger2
public class SwaggerConfig { @Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo());
} private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("分布式某某系统")
.description("某某系统接口文档说明")
.termsOfServiceUrl("http://localhost:8081")
.contact(new Contact("vker", "", "6492178@gmail.com"))
.version("1.0")
.build();
} @Bean
UiConfiguration uiConfig() {
return new UiConfiguration(null, "list", "alpha", "schema",
UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS, false, true, 60000L);
}
}
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 DocumentationConfig implements SwaggerResourcesProvider { @Override
public List<SwaggerResource> get() {
List<SwaggerResource> resources = new ArrayList<>();
//第一个参数随便写,中间的参数是你zuul中application中的配置路径,第三是版本信息,随意
resources.add(swaggerResource("随便填", "/api/user/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;
}
}

然后就是user的配置

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.2.RELEASE</version>
</dependency>
<--swagger2依赖-->
<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>
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.swagger2.annotations.EnableSwagger2; @Configuration
@EnableSwagger2
public class Swagger2Config {
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//为当前包下controller生成API文档
.apis(RequestHandlerSelectors.basePackage("com.cn.me.controller"))
//为有@Api注解的Controller生成API文档
// .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
//为有@ApiOperation注解的方法生成API文档
// .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build();
} private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("xxxxx")//自己定义标题
.description("部门、用户等接口")
.version("1.0")
.build();
}
}

最新文章

  1. 使用rsync同步目录
  2. [AS3.0] NetConnection.Connect.Rejected 解决办法
  3. 使用PPT绘制96孔板
  4. 消息推送之GCM
  5. nginx 多站点配置方法集合(转)
  6. 检查 CPU 是否支持二级地址转换 - 摘自网络
  7. Lua基础之字符串(string)
  8. 【转】让 cocos2d-x 的 CCHttpRequest 支持https
  9. JDBC连接池以及动态SQL处理
  10. Qt 学习之路 :菜单栏、工具栏和状态栏
  11. ch01.深入理解C#委托及原理(转)
  12. 带有OUTPUT的INSERT,DELETE,UPDATE
  13. 阿里开源的热补丁框架AndFix使用教程
  14. VS2017中的nuget还原失败或超时的解决方案
  15. [Swift]LeetCode957. N天后的牢房 | Prison Cells After N Days
  16. centos7.4安装redis
  17. 044、vloume声明周期管理(2019-03-07 周四)
  18. Linux - 操作系统
  19. Base64加密转换原理与代码实现
  20. LINUX的STRACE命令用法 [转]

热门文章

  1. 万字干货! 使用docker部署jenkins和gitlab
  2. LibreOJ 144. DFS 序 1
  3. day01-Spring基本介绍
  4. SpringBoot源码学习3——SpringBoot启动流程
  5. MySQL之字段约束条件
  6. Java 进阶P-4.6+P-4.7
  7. finally代码块-多异常的捕获处理
  8. python生成自动化测试报告并发送到指定邮箱
  9. IDEA手动导入jar包到maven本地库
  10. P4_创建第一个小程序项目