1、为什么要写Api文档

现在,前后端分离的开发模式已经非常流行,后端开发工程师只负责完成后端接口,前端页面的开发和渲染完全由前端工程师完成。

问题来了,前端工程师怎么知道后端接口的具体定义呢?答案是由后端工程师撰写。

2、写Api文档很头疼吗

答案是一定的,这对后端工程师来说,是额外的工作,编码已经很耗费精力了,这时前端工程师来催文档,不头疼才怪:),当然这也不是前端工程师的问题,都是为项目的进度着急。

3、Swagger2

现在好了,一个自动撰写Api文档的神器出现了,他就是 Swagger2,Swagger2通过美观的WEB界面展示了所有接口的定义,非常方便,还能直接在界面上进行测试调用,有助于调试。

后端工程师,定义好接口,加几个注解,便能自动生成在线接口定义,前端工程师可以实时的看到。

下面就来讲讲如何把 Swagger2 整合到项目中来。

4、接口介绍

这里以一个简单的接口作为例子,接口的功能是输入用户ID,获取用户数据。

5、引入依赖

        <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>

6、增加Swagger2配置类

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.swagger2.annotations.EnableSwagger2; @Configuration
@EnableSwagger2
public class Swagger2 { // swagger2配置
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2) // 指定api类型为swagger2
.apiInfo(apiInfo()) // 用于定义api文档汇总信息
.select()
.apis(RequestHandlerSelectors
.basePackage("cn.zhuifengren.controller")) // 指定扫描的controller包
.paths(PathSelectors.any()) // 所有controller
.build();
} private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("hello工程接口api") // 文档页标题
.contact(new Contact("hello",
"",
"")) // 联系人信息
.description("专为hello工程提供的api文档") // 详细信息
.version("1.0.0") // 文档版本号
.termsOfServiceUrl("") // 网站地址
.build();
} }

7、在Controller类中增加Swagger2注解

import cn.zhuifengren.model.Result;
import cn.zhuifengren.model.User;
import cn.zhuifengren.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
@RequestMapping("/user")
@Api(value = "UserController", tags = "用户接口")
public class UserController { @Autowired
private UserService userService; @GetMapping("/info/{id}")
@ApiOperation(value = "获取用户信息")
public Result<User> getUserInfo(@ApiParam(value = "用户ID") @PathVariable("id") String id) { User userInfo = userService.getUserInfo(id);
Result<User> result = new Result<>();
result.setCode(200);
result.setMessage("success");
result.setData(userInfo); return result;
} }

其中 @Api注解 放在类上,用于对类的描述,@ApiOperation注解 放在方法上,用于对接口方法的描述。

8、模型类中增加Swagger2注解

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; @ApiModel(value = "用户实体", description = "用户实体")
public class User { @ApiModelProperty(value = "用户id", name = "id", example = "1")
private String id;
@ApiModelProperty(value = "用户姓名", name = "name", example = "小李")
private String name;
@ApiModelProperty(value = "用户年龄", name = "age", example = "36")
private Integer age;

其中 @ApiModel注解 放在类上,用于对模型类的描述,@ApiModelProperty注解 放在属性上,用于对模型属性的描述。

9、启动项目查看效果

启动项目后,进入地址:http://localhost:8080/swagger-ui.html,即可看到Swagger2 WEB页面,点击【Try it out】按钮,还可以对接口进行测试,是不是很方便呢,赶快去用一下吧

有了Swagger2,再也不用为写Api文档头疼了的更多相关文章

  1. 集成 Swagger2 构建强大的 RESTful API 文档

    微信公众号:一个优秀的废人如有问题或建议,请后台留言,我会尽力解决你的问题. 前言 快过年了,不知道你们啥时候放年假,忙不忙.反正我是挺闲的,所以有时间写 blog.今天给你们带来 SpringBoo ...

  2. Spring Boot2 系列教程 (四) | 集成 Swagger2 构建强大的 RESTful API 文档

    前言 快过年了,不知道你们啥时候放年假,忙不忙.反正我是挺闲的,所以有时间写 blog.今天给你们带来 SpringBoot 集成 Swagger2 的教程. 什么是 Swagger2 Swagger ...

  3. SpringBoot_06_使用Swagger2构建强大的RESTful API文档

    二.参考资料 1.Spring Boot中使用Swagger2构建强大的RESTful API文档 2.

  4. Spring Boot中使用Swagger2构建强大的RESTful API文档

    由于Spring Boot能够快速开发.便捷部署等特性,相信有很大一部分Spring Boot的用户会用来构建RESTful API.而我们构建RESTful API的目的通常都是由于多终端的原因,这 ...

  5. 使用Swagger2构建强大的RESTful API文档(1)(二十二)

    由于Spring Boot能够快速开发.便捷部署等特性,相信有很大一部分Spring Boot的用户会用来构建RESTful API.而我们构建RESTful API的目的通常都是由于多终端的原因,这 ...

  6. Spring Boot 中使用 Swagger2 构建强大的 RESTful API 文档

    项目现状:由于前后端分离,没有很好的前后端合作工具. 由于接口众多,并且细节复杂(需要考虑不同的HTTP请求类型.HTTP头部信息.HTTP请求内容等),高质量地创建这份文档本身就是件非常吃力的事,下 ...

  7. Spring Boot教程(二十二)使用Swagger2构建强大的RESTful API文档(1)

    由于Spring Boot能够快速开发.便捷部署等特性,相信有很大一部分Spring Boot的用户会用来构建RESTful API.而我们构建RESTful API的目的通常都是由于多终端的原因,这 ...

  8. SpringBoot使用Swagger2搭建强大的RESTful API 文档功能

    swagger用于定义API文档. Swagger2的使用 Maven Plugin添加Swagger2相关jar包 <!--swagger2 start--> <dependenc ...

  9. 使用Swagger2构建强大的RESTful API文档(2)(二十三)

    添加文档内容 在完成了上述配置后,其实已经可以生产文档内容,但是这样的文档主要针对请求本身,而描述主要来源于函数等命名产生,对用户并不友好,我们通常需要自己增加一些说明来丰富文档内容.如下所示,我们通 ...

随机推荐

  1. Nginx的安装和部署

    Nginx简介 Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行.其特点是占有内存少,并发能力强,事实上nginx的并发 ...

  2. CSS 世界中的方位与顺序

    在 CSS 中,我们经常会与各种方向方位打交道. 譬如 margin.padding,它们就会有 margin-left.margin-right 或者是 padding-left.padding-r ...

  3. 一文读懂k8s rbac 权限验证

    自我认为的k8s三大难点:权限验证,覆盖网络,各种证书. 今天就说一下我所理解的权限验证rbac. 咱不说rbac0,rbac1,rbac2,rbac3.咱就说怎么控制权限就行. 一.前言 1,反正R ...

  4. Kettle——shell交互命令

    Kettle--shell交互命令 在kettle上开发了job或transform可以以单独的文件存在,也可以存放在资源库中.调用这些程序可以通过shell脚本调用,记录下: 资源库中的job: . ...

  5. 分别在Update和FixedUpdate使用GetKeyDown

    测试目的 探究分别在Update和FixedUpdate使用GetKeyDown执行次数,会不同的 测试开始 在Update测试 我们先在Update测试,很正常是一帧重置一下状态,以防止点击一下执行 ...

  6. 【模拟】选数 luogu-1037

    AC代码 #include <bits/stdc++.h> using namespace std; #define ms(a,b) memset(a,b,sizeof(a)) typed ...

  7. Linux基础服务搭建综合

    Linux服务综合搭建的文章目录 =============================================== 1.foundation创建yum仓库 2.部署DNS 3.将YUM源 ...

  8. TypeError: attrib() got an unexpected keyword argument 'convert'

    使用pyinstaller -F aaa.py时,报错 TypeError: attrib() got an unexpected keyword argument 'convert' 没有exe生成 ...

  9. 阿里云IoT初试

    本文从概念到实战,以一个假想产品--"电子货架标签"(Electronic Shelf Label,以下简称ESL)为例,介绍基于阿里云IoT的物联网应用开发. 数据交互流程 以云 ...

  10. WIN XP SP2系统经常性死机问题解决历程

    如题: 1.初始时,XP还能进入系统,等系统3分钟左右,鼠标熄灭,键盘无反应,查看资源管理器CPU 100%,内存占用不高. 2.现象初步分析: a.怀疑是病毒占用CPU 100%,于是下载360安全 ...