转载请标明出处:

原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot10-springrestdocs/

本文出自方志朋的博客

这篇文章将带你了解如何用spring官方推荐的restdoc去生成api文档。本文创建一个简单的springboot工程,将http接口通过Api文档暴露出来。只需要通过 JUnit单元测试和Spring的MockMVC就可以生成文档。

准备工作

  • 你需要15min
  • Jdk 1.8
  • maven 3.0+
  • idea

创建工程

引入依赖,其pom文件:

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.springframework.restdocs</groupId>
<artifactId>spring-restdocs-mockmvc</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

通过@SpringBootApplication,开启springboot

@SpringBootApplication
public class Application { public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

在springboot通常创建一个controller:

@RestController
public class HomeController { @GetMapping("/")
public Map<String, Object> greeting() {
return Collections.singletonMap("message", "Hello World");
} }

启动工程,访问localhost:8080,浏览器显示:

{“message”:“Hello World”}

证明接口已经写好了,但是如何通过restdoc生存api文档呢

Restdoc,通过单元测试生成api文档

restdocs是通过单元测试生存snippets文件,然后snippets根据插件生成htm文档的。

建一个单元测试类:

@RunWith(SpringRunner.class)
@WebMvcTest(HomeController.class)
@AutoConfigureRestDocs(outputDir = "target/snippets")
public class WebLayerTest { @Autowired
private MockMvc mockMvc; @Test
public void shouldReturnDefaultMessage() throws Exception {
this.mockMvc.perform(get("/")).andDo(print()).andExpect(status().isOk())
.andExpect(content().string(containsString("Hello World")))
.andDo(document("home"));
}
}

其中,@ AutoConfigureRestDocs注解开启了生成snippets文件,并指定了存放位置。

启动单元测试,测试通过,你会发现在target文件下生成了一个snippets文件夹,其目录结构如下:

└── target
└── snippets
└── home
└── httpie-request.adoc
└── curl-request.adoc
└── http-request.adoc
└── http-response.adoc

默认情况下,snippets是Asciidoctor格式的文件,包括request和reponse,另外其他两种httpie和curl两种流行的命令行的http请求模式。

到目前为止,只生成了Snippets文件,需要用Snippets文件生成文档。

怎么用Snippets

创建一个新文件src/main/asciidoc/index.adoc :

= 用 Spring REST Docs 构建文档

This is an example output for a service running at http://localhost:8080:

.request
include::{snippets}/home/http-request.adoc[] .response
include::{snippets}/home/http-response.adoc[] 这个例子非常简单,通过单元测试和一些简单的配置就能够得到api文档了。

adoc的书写格式,参考:http://docs.spring.io/spring-restdocs/docs/current/reference/html5/,这里不多讲解。

需要使用asciidoctor-maven-plugin插件,在其pom文件加上:

<plugin>
<groupId>org.asciidoctor</groupId>
<artifactId>asciidoctor-maven-plugin</artifactId>
<executions>
<execution>
<id>generate-docs</id>
<phase>prepare-package</phase>
<goals>
<goal>process-asciidoc</goal>
</goals>
<configuration>
<sourceDocumentName>index.adoc</sourceDocumentName>
<backend>html</backend>
<attributes>
<snippets>${project.build.directory}/snippets</snippets>
</attributes>
</configuration>
</execution>
</executions>
</plugin>

这时只需要通过mvnw package命令就可以生成文档了。

在/target/generated-docs下有个index.html,打开这个html,显示如下,界面还算简洁:

结语

通过单元测试,生存adoc文件,再用adoc文件生存html,只需要简单的几步就可以生成一个api文档的html文件,这个html文件你可以通网站发布出去。整个过程很简单,对代码无任何影响。

源码下载:https://github.com/forezp/SpringBootLearning

参考资料

restdocs

http://docs.spring.io/spring-restdocs/docs/current/reference/html5/




扫码关注公众号有惊喜

(转载本站文章请注明作者和出处 方志朋的博客

SpringBoot非官方教程 | 第十篇: 用spring Restdocs创建API文档的更多相关文章

  1. SpringBoot入门教程(二十)Swagger2-自动生成RESTful规范API文档

    Swagger2 方式,一定会让你有不一样的开发体验:功能丰富 :支持多种注解,自动生成接口文档界面,支持在界面测试API接口功能:及时更新 :开发过程中花一点写注释的时间,就可以及时的更新API文档 ...

  2. Spring 官方教程:使用 Restdocs 创建 API 文档

    https://mp.weixin.qq.com/s?__biz=MzU0MDEwMjgwNA==&mid=2247483998&idx=1&sn=6ae5fa795d36b1 ...

  3. SpringBoot非官方教程 | 第二十篇: 处理表单提交

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot-form/ 本文出自方志朋的博客 这篇文件主要介 ...

  4. SpringBoot非官方教程 | 第二十二篇: 创建含有多module的springboot工程

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springbot22-modules/ 本文出自方志朋的博客 这篇文 ...

  5. (转) SpringBoot非官方教程 | 第二十四篇: springboot整合docker

    这篇文篇介绍,怎么为 springboot程序构建一个Docker镜像.docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从Apache2.0协议开源.Docker 可以让开发者打包他们的 ...

  6. SpringBoot非官方教程 | 第二十六篇: sprinboot整合elk,搭建实时日志平台

    转载请标明出处: 原文首发于https://www.fangzhipeng.com/springboot/2017/07/11/sprinboot25-elk/ 本文出自方志朋的博客 这篇文章主要介绍 ...

  7. SpringBoot非官方教程 | 第二十四篇: springboot整合docker

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot24-docker/ 本文出自方志朋的博客 这篇文 ...

  8. (转)SpringBoot非官方教程 | 第十二篇:springboot集成apidoc

    首先声明下,apidoc是基于注释来生成文档的,它不基于任何框架,而且支持大多数编程语言,为了springboot系列的完整性,所以标了个题. 一.apidoc简介 apidoc通过在你代码的注释来生 ...

  9. SpringBoot非官方教程 | 第十二篇:springboot集成apidoc

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot-apidoc/ 本文出自方志朋的博客 首先声明下 ...

随机推荐

  1. Git使用教程,感觉比较全,所以【转载】

    一:Git是什么? Git是目前世界上最先进的分布式版本控制系统. 二:SVN与Git的最主要的区别? SVN是集中式版本控制系统,版本库是集中放在中央服务器的,而干活的时候,用的都是自己的电脑,所以 ...

  2. 深入理解JavaScript系列(19):求值策略(Evaluation strategy)

    介绍 本章,我们将讲解在ECMAScript向函数function传递参数的策略. 计算机科学里对这种策略一般称为“evaluation strategy”(大叔注:有的人说翻译成求值策略,有的人翻译 ...

  3. C# 安装WindowService服务和相关

    https://www.cnblogs.com/charlie-chen2016/p/8031774.html 这是一个备份数据库的服务,逻辑很简单,就是通过定时器实现在特定的时间执行SQL语句备份数 ...

  4. MySQL8.0加载文件内容报错: ERROR 1148: The used command is not allowed with this MySQL version

    mysql数据库将文件内容加载到表中报错: mysql> LOAD DATA LOCAL INFILE '/path/pet.txt' INTO TABLE pet LINES TERMINAT ...

  5. js动画杂记

    在画布上做动画 方法有 setIntervel(function(){},time); setTimeout(function(){},time); 新方法 window.requestAnimati ...

  6. webstorm 配置Vue.js 语法提示

    标签属性 v-text v-html v-once v-if v-show v-else v-for v-on v-bind v-model v-ref v-el v-pre v-cloak v-on ...

  7. R语言计算相关矩阵然后将计算结果输出到CSV文件

    R语言计算出一个N个属性的相关矩阵(),然后再将相关矩阵输出到CSV文件. 读入的数据文件格式如下图所示: R程序采用如下语句: data<-read.csv("I:\\SB\land ...

  8. show_sql和format_sql

    <property name="show_sql">true</property> <property name="hibernate.fo ...

  9. 排查在 Azure 中创建新 Linux 虚拟机时遇到的 Resource Manager 部署问题

    本文内容 常见问题 收集活动日志 问题:自定义映像:预配错误 问题:自定义/库/应用商店映像:分配失败 后续步骤 尝试创建新的 Azure 虚拟机 (VM) 时,遇到的常见错误是预配失败或分配失败. ...

  10. WAKE-LINUX-SOFT-linux安装,配置,基础

    1,ubuntu 1,1下载,安装 中文ubuntu站,http://cn.ubuntu.com/ 下载地址:https://www.ubuntu.com/download 安装手册:https:// ...