创建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 ...
随机推荐
- [Bzoj1767][Ceoi2009]harbingers (树上斜率优化)
1767: [Ceoi2009]harbingers Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 451 Solved: 120[Submit][S ...
- Html.EditorFor 加 htmlAttributes
@Html.EditorFor(m => m.Name, new { htmlAttributes = new { @required = "true", @anotherA ...
- Linux命令chattr和lsattr
先看字面解释: chattr:chattr - change file attributes on a Linux file system lsattr - list file attributes ...
- 【python】SHA1 算法
http://blog.163.com/sh_wenfen/blog/static/99708242007231103936938/
- HNU 12834 Thread Tree
递归输出即可了 #include<bits/stdc++.h> using namespace std; struct tree{ int dot; string s; } ...
- 【原创】PHP扩展开发入门
PHP扩展开发入门 作者:wf (360电商技术组) 在我们编写自己的第一个php扩展之前,先了解一下php的总体架构和执行机制. php的架构如图1所看到的. 当中一个重要的就是SAPI(serve ...
- Android Activity与远程Service的通信学习总结
当一个Service在androidManifest中被声明为 process=":remote", 或者是还有一个应用程序中的Service时,即为远程Service, 远程的意 ...
- glTF格式初步了解
glTF格式初步了解 近期看到Qt 3D的进展.偶然了解到了一种新的格式:glTF格式.这样的格式据说比现有的3D格式更加符合OpenGL应用的须要.这引起了我的好奇.于是我在Qt 3D的外部链接中找 ...
- win7下装ubuntu双系统后无法进入win7的解决方法
本来电脑的系统是win7,然后用u盘装了ubuntu之后可能会出现开机没有引导界面而直接进入ubuntu系统的情况. 原因:没有设置gurb引导 解决方法:需要更新gurb来使ubuntu识别出win ...
- 浅谈c#的三个高级参数ref out 和Params C#中is与as的区别分析 “登陆”与“登录”有何区别 经典SQL语句大全(绝对的经典)
浅谈c#的三个高级参数ref out 和Params c#的三个高级参数ref out 和Params 前言:在我们学习c#基础的时候,我们会学习到c#的三个高级的参数,分别是out .ref 和 ...