创建swagger的springboot-stater,并在spring cloud zuul网关中引入
Swagger 是一款RESTFUL接口的、基于YAML、JSON语言的文档在线自动生成、代码自动生成的工具。
通过在controller中添加注解,即可轻易实现代码文档化。
Swagger提供ui界面,方便查看接口说明和测试接口功能。
本文主要讲解如何创建一个swagger 的springboot starter项目,只要在其他服务中引入该starter.并添加相关注解,即可完成接口文档化。
并讲解了如何在spring cloud zuul网关中引入swagger,为前端提供统一的访问入口。
本文的完整代码:GitHub:swagger-starter。
创建sawgger-springboot-starter项目
POM配置
pom引入swagger依赖
<!-- swagger 依赖 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
pom引入springboot starter相关依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
swagger配置
相关的配置属性,将会在yml文件中进行配置
package com.swagger.config; import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties; @Data
@ConfigurationProperties(prefix = "swagger")
public class SwaggerProperties { //需要扫描的包路径
private String basePackage = "com";
//显示的标题
private String title = "swagger";
//联系人
private String contactName;
//联系地址
private String contactUrl;
//联系邮件地址
private String contactEmail;
//文档版本号
private String version;
//描述
private String description;
//
private String termsOfServiceUrl; private String license;
private String licenseUrl; }
配置类
package com.swagger.config; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.web.UiConfiguration; /**
*功能描述
* @author lgj
* @Description swagger 配置类
* @date 6/5/19
*/
@Slf4j
@Configuration
@EnableConfigurationProperties(SwaggerProperties.class)
public class SwaggerConfig { @Autowired
private SwaggerProperties swaggerProperties; @Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//为当前包路径
.apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage()))
.paths(PathSelectors.any())
.build();
}
//构建 api文档的详细信息函数,注意这里的注解引用的是哪个
private ApiInfo apiInfo() { log.info("swaggerProperties = " + swaggerProperties);
return new ApiInfoBuilder()
//页面标题
.title(swaggerProperties.getTitle())
//创建人
.contact(new Contact(swaggerProperties.getContactName(), swaggerProperties.getContactUrl(), swaggerProperties.getContactEmail()))
//版本号
.version(swaggerProperties.getVersion())
//描述
.description(swaggerProperties.getDescription())
.license(swaggerProperties.getLicense())
.licenseUrl(swaggerProperties.getLicenseUrl())
.termsOfServiceUrl(swaggerProperties.getTermsOfServiceUrl())
.build()
;
} @Bean
UiConfiguration uiConfig() {
return new UiConfiguration(null, "list", "alpha", "schema",
UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS, false, true, 60000L);
} }
既然作为starter,还需要指定项目启动时的配置类,因为其他项目引用时没有指定扫描该类,那么就不会创建相关的bean。
因此需要在starter中指定。
在resource中创建文件 META-INF/spring.factories

spring.factories文件内容,通过EnableAutoConfiguration指定。
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.swagger.config.SwaggerConfig
swagger-springboot-starter创建完成。
创建一个WEB应用进行测试
引入上面starter的依赖
<dependency>
<groupId>com.swagger</groupId>
<artifactId>swagger-springboot-starter</artifactId>
<version>1.0.0</version>
</dependency>
yml中进行相关的配置
server:
port: 8900 swagger:
basePackage: com
title: "demo ui doc"
contactName: "libai"
contactUrl: contactUrl-demo
contactEmail: contactEmail-demo
version: v1.0.0
description: description-demo
termsOfServiceUrl: termsOfServiceUrl-demo
license: license-demo
licenseUrl: licenseUrl-demo
创建一个controller
package com.demo.demo; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import java.util.Date; @Api(value = "/demo",description = "demo controller")
@RestController
@RequestMapping("/demo")
public class WebController { @ApiOperation(value = "/demo/1",notes="这是demo1",tags="{tag1,tag2}",response=String.class,httpMethod= "GET")
@ApiParam(name = "name",value = "libai")
@GetMapping("/1")
public String demo1(String name){ return name + ":" + new Date().toString();
} @ApiOperation(value = "/demo/2",notes="这是demo2",tags="{tag3,tag4}",response=String.class,httpMethod= "POST")
@PostMapping("/2")
public String demo2(String name){ return name + ":" + new Date().toString();
}
}
注意这里使用@Api和@ApiOperation,@ApiParam等实现接口说明。
主要说明相关的类和方法。以便swagger-ui查看到。
更多使用方法参考本博文
在启动类上添加注解@EnableSwagger2
@EnableSwagger2
@SpringBootApplication
public class DemoApplication { public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
} }
测试
启动DemoApplication应用,访问swagger-ui界面,地址http://localhost:8900/swagger-ui.html
可以看到之前代码中的相关配置。

同时,它还可以实现对接口的测试

SpringCloud Zuul中引入
在使用spring cloud进行开发时,如果每个微服务都引入sawgger,每个微服务都是单独的访问地址,如果有几百个微服务,对swagger进行管理访问将会比较麻烦。因此需要在zuul网关提供统一的访问入口。
pom中引入zuul和swagger依赖
<!-- swagger 依赖 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-zuul -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
yml中配置
server:
port: 8902 zuul:
routes:
#demo系统
demo:
path: /demo/**
url: http://localhost:8900
yml中配置的是demo应用的路由配置,当访问 http://localhost:8902/demo/**/** 时,将会转发到http://localhost:8900.也就是demo应用。
swagger 资源配置
package com.zuul.demo.swagger; import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
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; /**
*功能描述
* @author lgj
* @Description
* @date 6/5/19
*/ @EnableSwagger2
@Component
@Primary
public class SwaggerResources implements SwaggerResourcesProvider {
@Override
public List<SwaggerResource> get() {
List resources = new ArrayList<>();
resources.add(swaggerResource("demo模块", "/demo/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;
}
}
swaggerResource("demo模块", "/demo/v2/api-docs", "2.0")
注意这里的location值,对应的是yml中的配置/demo + /v2/api-docs,可修改的是参数name。
demo:
path: /demo/**
url: http://localhost:8900
如果有新模块需要添加
user-app:
path: /user/**
url: http://localhost:8903
只需要在这里添加如下代码即可。
resources.add(swaggerResource("user模块", "/user/v2/api-docs", "2.0"));
启动类
package com.zuul.demo; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy; @EnableZuulProxy
@SpringBootApplication
public class ZuulApplication { public static void main(String[] args) {
SpringApplication.run(ZuulApplication.class, args);
} }
测试
启动zuul应用,访问地址:http://localhost:8902/swagger-ui.html
在右上角的"Select a spec"可以选择查看对应模块的API文档。

如果出现以下错误,则说明没有添加注解@EnableSwagger2

完成!!!!!!!!!!!!!!
创建swagger的springboot-stater,并在spring cloud zuul网关中引入的更多相关文章
- Spring cloud Zuul网关异常处理
Spring cloud Zuul网关异常处理 一 异常测试: 1> 创建一个pre类型的过滤器,并在该过滤器的run方法实现中抛出一个异常.比如下面的实现,在run方法中调用的doSometh ...
- Spring Cloud Zuul网关 Filter、熔断、重试、高可用的使用方式。
时间过的很快,写springcloud(十):服务网关zuul初级篇还在半年前,现在已经是2018年了,我们继续探讨Zuul更高级的使用方式. 上篇文章主要介绍了Zuul网关使用模式,以及自动转发机制 ...
- Spring Cloud Zuul 网关使用与 OAuth2.0 认证授权服务
API 网关的出现的原因是微服务架构的出现,不同的微服务一般会有不同的服务地址,而外部客户端可能需要调用多个服务的接口才能完成一个业务需求,如果让客户端直接与各个微服务通信,会有以下的问题: 客户端会 ...
- Spring Cloud Zuul 网关服务的fallback
当我们的zuul进行路由分发时,如果后端服务没有启动,或者调用超时,这时候我们希望Zuul提供一种降级功能,而不是将异常暴露出来. Spring cloud zuul提供这种降级功能,操作步骤如下: ...
- Spring Cloud Zuul 网关的分布式系统中整合Swagger(转)和 zuul跨域访问问题
首先恭喜自己终于找对了努力的方向,很荣幸能在公司接触到微服务架构,也很高兴公司一个大佬哥们愿意带我,他技术确实很牛逼,我也很佩服他,前后端通吃,干了六年能有这样的水平.最近跟着在搞微服务架构,给我分配 ...
- Spring Cloud(十一):Spring Cloud Zuul网关 Filter、熔断、重试、高可用的使用方式
上篇文章主要介绍了Zuul网关使用模式,以及自动转发机制,但其实Zuul还有更多的应用场景,比如:鉴权.流量转发.请求统计等等,这些功能都可以使用Zuul来实现. Zuul的核心 Filter是Zuu ...
- Spring Cloud zuul网关服务 一
上一篇进行Netflix Zuul 1.0 与 gateway的对比.今天来介绍一下 zuul的搭建及应用 Zuul 工程创建 工程创建 cloud-gateway-zuul.还是基于之前的工程 po ...
- spring cloud深入学习(十二)-----Spring Cloud Zuul网关 Filter、熔断、重试、高可用的使用方式
Zuul的核心 Filter是Zuul的核心,用来实现对外服务的控制.Filter的生命周期有4个,分别是“PRE”.“ROUTING”.“POST”.“ERROR”,整个生命周期可以用下图来表示. ...
- Spring Cloud Zuul网关(快速搭建)
zuul 是netflix开源的一个API Gateway 服务器, 本质上是一个web servlet应用. 在云平台上提供动态路由,监控,弹性,安全等边缘服务的框架.相当于是设备和 Netflix ...
随机推荐
- web.py 使用 db.select 返回的数据只能遍历一次
2013-10-05 23:04:33| 1. web.py 使用 db.select 返回的数据只能遍历一次import webdb = web.database(dbn='mysql', db ...
- loj517 计算几何瞎暴力(Trie树)
题目: https://loj.ac/problem/517 分析: 操作4比较特殊,我们先来分析下操作4 操作4相当于需要一个数据结构,使得里面的数据有序(这有很多选择) 结合操作1,操作4的“排序 ...
- LINUX 下 ipv6 socket 编程
大家都知道,随着互联网上主机数量的增多,现有的32位IP地址已经不够用了,所以推出了下一代IP地址IPv6,写网络程序的要稍微改变一下现有的网络程序适应IPv6网络是相当容易的事.对于我们来说就是IP ...
- 设置eclipse默认用户名
在使用Eclipse自动生成注释时,用户名总会被设置成Windows的登陆用户名.但大多数时候Windows用户名并不是我们写到注释里的用户名. 为了不想一个文件一个文件地修改,可以在最初时就设设置好 ...
- MySQL中的数据类型的长度范围和显示宽度(转)
长度范围是随数据类型就已经是固定的值,而显示宽度与长度范围无关. 以下是每个整数类型的存储和范围(来自MySQL手册) 类型 字节 最小值 最大值 (带符号的/无符号的) (带符号的/无符号的) TI ...
- 限制input的输入类型
1.只能输入和粘贴汉字 <input onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')" onbeforepaste= ...
- volatile关键字解析&内存模型&并发编程中三概念
原文链接: http://www.cnblogs.com/dolphin0520/p/3920373.html volatile这个关键字可能很多朋友都听说过,或许也都用过.在Java5之前,它是一个 ...
- POI 的使用
POI 使用 一. POI简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能..NET的开发人员则 ...
- sysinfo 系统调用
在分析luci时,看到 usr/lib/luci/sys.lua 里调用 nixio.sysinfo().这是一个c调用lua的用例.在nixio的代码process.c里导出了给lua调用的接口.在 ...
- jQuery操作Form表单元素
Web开发中常常须要操作表单,form表单元素有select.checkbox.radio.textarea.button.file.text.hidden.password等. 当中checkbox ...