传统的API文档编写存在以下几个痛点:

对API文档进行更新的时候,需要通知前端开发人员,导致文档更新交流不及时;

API接口返回信息不明确

大公司中肯定会有专门文档服务器对接口文档进行更新。

缺乏在线接口测试,通常需要使用相应的API测试工具,比如postman、SoapUI等

接口文档太多,不便于管理

为了解决传统API接口文档维护的问题,为了方便进行测试后台Restful接口并实现动态的更新,因而引入Swagger接口工具。

Swagger具有以下优点

1.功能丰富:支持多种注解,自动生成接口文档界面,支持在界面测试API接口功能;

2.及时更新:开发过程中花一点写注释的时间,就可以及时的更新API文档,省心省力;

3.整合简单:通过添加pom依赖和简单配置,内嵌于应用中就可同时发布API接口文档界面,不需要部署独立服务。

springboot集成

Maven依赖信息

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<!-- 管理依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.M7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- SpringBoot整合Web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- SpringBoot整合eureka客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
</dependencies>
<!-- 注意: 这里必须要添加, 否者各种依赖有问题 -->
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>

SwaggerConfig

@Configuration
@EnableSwagger2
public class SwaggerConfig { @Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
// api扫包
.apis(RequestHandlerSelectors.basePackage("com.itmayiedu.api")).paths(PathSelectors.any()).build();
} private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("电商系统").description("Java分布式")
.termsOfServiceUrl("http://www")
// .contact(contact)
.version("1.0").build();
} }

访问地址:http://localhost:8060/swagger-ui.html#/swagger-controller

Zull整合Swagger管理微服务所有API

会员和订单引入Maven依赖

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

application.yml配置

Api接口扫描范围

swagger:
base-package: com.itmayeidu.api

ZuulGateway网关

@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
@EnableSwagger2Doc
public class AppGateWay { // @EnableZuulProxy 开启网关代理 public static void main(String[] args) {
SpringApplication.run(AppGateWay.class, args);
} // zuul配置能够使用config实现实时更新
@RefreshScope
@ConfigurationProperties("zuul")
public ZuulProperties zuulProperties() {
return new ZuulProperties();
} // 添加文档来源
@Component
@Primary
class DocumentationConfig implements SwaggerResourcesProvider {
@Override
public List<SwaggerResource> get() {
List resources = new ArrayList<>();
// app-itmayiedu-order
resources.add(swaggerResource("app-itmayiedu-member", "/api-member/v2/api-docs", "2.0"));
resources.add(swaggerResource("app-itmayiedu-order", "/api-order/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;
}
} }

Maven依赖信息

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

Swagger 2.0 集成配置的更多相关文章

  1. eclipse3.7+resin4.0集成配置小结

    1.插件不要用improve公司的了,那个太老了.直接用resin官方的,用eclipse的help->install new software功能,地址用:http://www.caucho. ...

  2. spring boot:swagger3的安全配置(swagger 3.0.0 / spring security / spring boot 2.3.3)

    一,swagger有哪些环节需要注意安全? 1,生产环境中,要关闭swagger application.properties中配置: springfox.documentation.swagger- ...

  3. netcore3.0 webapi集成Swagger 5.0,Swagger使用

    Swagger使用 1.描述 Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务. 作用: 1.接口的文档在线自动生成. 2.功能测试 本文转自 ...

  4. SpringBoot2.0集成FastDFS

    SpringBoot2.0集成FastDFS 前两篇整体上介绍了通过 Nginx 和 FastDFS 的整合来实现文件服务器.但是,在实际开发中对图片或文件的操作都是通过应用程序来完成的,因此,本篇将 ...

  5. swagger的说明、配置及使用

    一.What is swagger? 官方介绍:Swagger是一个规范且完整的框架,提供描述.生产.消费和可视化RESTful Web Service.专业角度:Swagger是由庞大工具集合支撑的 ...

  6. 3、集成springfox-swagger 3.0.0 + 集成knife4j

    集成springfox-swagger 3.0.0 + 集成knife4j 1.基本用法 1.1.导入依赖 <!-- web接口 --> <dependency> <gr ...

  7. GitLab CI持续集成配置方案(补)

    上篇文章介绍了GitLab CI的持续集成配置方法,本篇文章将主要介绍NUnit的持续集成和遇到的一些坑 1.NUnit单元测试持续集成 下载NUnit.3.4.1.msi,https://githu ...

  8. maven2 + tomcat6 + eclipse集成配置

    转载:http://wenku.baidu.com/view/d64147c676eeaeaad1f330d4.html?re=view /*maven2 + tomcat6 + eclipse集成配 ...

  9. SpringMVC + spring3.1.1 + hibernate4.1.0 集成及常见问题总结

    下载地址: http://pan.baidu.com/s/1qWDinyk 一 开发环境 1.动态web工程 2.部分依赖 hibernate-release-4.1.0.Final.zip hibe ...

随机推荐

  1. Gym:101630J - Journey from Petersburg to Moscow(最短路)

    题意:求1到N的最短路,最短路的定义为路径上最大的K条边. 思路:对于每种边权,假设为X,它是第K大,那么小于X的变为0,大于K的,边权-X.然后求最短路,用dis[N]+K*X更新答案. 而小于K的 ...

  2. BZOJ-4327:JSOI2012 玄武密码(AC自动机模板题)

    在美丽的玄武湖畔,鸡鸣寺边,鸡笼山前,有一块富饶而秀美的土地,人们唤作进香河.相传一日,一缕紫气从天而至,只一瞬间便消失在了进香河中.老人们说,这是玄武神灵将天书藏匿在此.  很多年后,人们终于在进香 ...

  3. MySQL业务-发放的优惠券 用户使用情况_20161028

    运营部门给用户发放优惠券,如果想监控优惠券的使用效果 优惠券使用率是个反映效果的很好指标 下面sql就是针对某天对特定用户发放的优惠券在发放日期以后每天的使用情况 SELECT e.城市,e.用户ID ...

  4. robotium 测试APK<一> 建立测试工程

    1.准备Android开发环境 2.准备签名工具http://www.troido.de/re-sign.jar 1.建立测试工程 打开Eclipse,点击File->New一个Android ...

  5. AtCoder Grand Contest 013 C:Ants on a Circle

    题目传送门:https://agc013.contest.atcoder.jp/tasks/agc013_c 题目翻译 给你一个周长为\(L\)的圆,有\(N\)只蚂蚁在圆上爬,速度为一单位距离每秒. ...

  6. BZOJ4408:[FJOI2016]神秘数

    浅谈主席树:https://www.cnblogs.com/AKMer/p/9956734.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem.p ...

  7. 安装pillow

    最近想学Python的图像操作.首要任务就是安装pillow.这个强大的图形处理工具. 但是我遇到了一个问题. Collecting pilow  Could not find a version t ...

  8. ThinkPHP隐藏index.php出现No input file specified的解决方法

    因为在Fastcgi模式下,php不支持rewrite的目标网址的PATH_INFO的解析 ThinkPHP运行在URL_MODEL=2时,会出现 No input file specified.的情 ...

  9. xgene:WGS,突变与癌,RNA-seq,WES

     人类全基因组测序06 SNP(single nucleotide polymorphism):有了10倍以上的覆盖深度以后,来确认SNP信息,就相当可靠了. 一个普通黄种人的基因组,与hg19这个参 ...

  10. Elasticsearch检索分类详解

    前言 Elasticsearch中当我们设置Mapping(分词器.字段类型)完毕后,就可以按照设定的方式导入数据. 有了数据后,我们就需要对数据进行检索操作.根据实际开发需要,往往我们需要支持包含但 ...