文章出处: http://blog.didispace.com/swagger2markup-asciidoc/

说明

项目中使用Swagger之后,我们能够很轻松的管理API文档,并非常简单的模拟接口调用,但是构建的文档必须通过在项目中整合 swagger-ui、或使用单独部署的 swagger-ui/v2/api-docs返回的配置信息才能展现出您所构建的API文档。本文将在使用Swagger的基础上,再介绍一种生成静态API文档的方法,以便于构建更轻量部署和使用的API文档。

Swagger2Markup简介

Swagger2Markup是Github上的一个开源项目。该项目主要用来将Swagger自动生成的文档转换成几种流行的格式以便于静态部署和使用,比如:AsciiDoc、Markdown、Confluence。

项目主页:https://github.com/Swagger2Markup/swagger2markup

如何使用

在使用Swagger2Markup之前,我们先需要准备一个使用了Swagger的Web项目,可以是直接使用Swagger2的项目

  1. 引入依赖

     <dependency>
    <groupId>io.github.swagger2markup</groupId>
    <artifactId>swagger2markup</artifactId>
    <version>1.3.1</version>
    </dependency> <repositories>

    <repository>

    <snapshots>

    <enabled>true</enabled>

    <updatePolicy>always</updatePolicy>

    </snapshots>

    <id>jcenter-releases</id>

    <name>jcenter</name>

    <url>http://jcenter.bintray.com</url>

    </repository>

    </repositories>

  1. 编写一个单元测试用例来生成执行生成文档的代码

     @RunWith(SpringRunner.class)
    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
    public class SwaggerDemoApplicationTests {
     <span class="hljs-comment">/**
    * 生成AsciiDocs格式文档
    * </span><a href="https://github.com/throws" title="@throws" class="at-link"><span class="hljs-comment"><span class="hljs-doctag">@throws</span></span></a><span class="hljs-comment"> Exception
    */</span>
    <a href="https://github.com/Test" title="@Test" class="at-link"><span class="hljs-meta">@Test</span></a>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">generateAsciiDocs</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> Exception </span>{
    <span class="hljs-comment">// 输出Ascii格式</span>
    Swagger2MarkupConfig config = <span class="hljs-keyword">new</span> Swagger2MarkupConfigBuilder()
    .withMarkupLanguage(MarkupLanguage.ASCIIDOC)
    .withOutputLanguage(Language.ZH)
    .withPathsGroupedBy(GroupBy.TAGS)
    .withGeneratedExamples()
    .withoutInlineSchema()
    .build(); Swagger2MarkupConverter.from(<span class="hljs-keyword">new</span> URL(<span class="hljs-string">"http://localhost:8080/v2/api-docs"</span>))
    .withConfig(config)
    .build()
    .toFolder(Paths.get(<span class="hljs-string">"./docs/asciidoc/generated"</span>));
    } <span class="hljs-comment">/**
    * 生成Markdown格式文档
    * </span><a href="https://github.com/throws" title="@throws" class="at-link"><span class="hljs-comment"><span class="hljs-doctag">@throws</span></span></a><span class="hljs-comment"> Exception
    */</span>
    <a href="https://github.com/Test" title="@Test" class="at-link"><span class="hljs-meta">@Test</span></a>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">generateMarkdownDocs</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> Exception </span>{
    <span class="hljs-comment">// 输出Markdown格式</span>
    Swagger2MarkupConfig config = <span class="hljs-keyword">new</span> Swagger2MarkupConfigBuilder()
    .withMarkupLanguage(MarkupLanguage.MARKDOWN)
    .withOutputLanguage(Language.ZH)
    .withPathsGroupedBy(GroupBy.TAGS)
    .withGeneratedExamples()
    .withoutInlineSchema()
    .build(); Swagger2MarkupConverter.from(<span class="hljs-keyword">new</span> URL(<span class="hljs-string">"http://localhost:8080/v2/api-docs"</span>))
    .withConfig(config)
    .build()
    .toFolder(Paths.get(<span class="hljs-string">"./docs/markdown/generated"</span>));
    }

        /**
* 生成Confluence格式文档
* @throws Exception
*/
@Test
public void generateConfluenceDocs() throws Exception {
// 输出Confluence使用的格式
Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder()
.withMarkupLanguage(MarkupLanguage.CONFLUENCE_MARKUP)
.withOutputLanguage(Language.ZH)
.withPathsGroupedBy(GroupBy.TAGS)
.withGeneratedExamples()
.withoutInlineSchema()
.build(); Swagger2MarkupConverter.from(new URL("http://localhost:8080/v2/api-docs"))
.withConfig(config)
.build()
.toFolder(Paths.get("./docs/confluence/generated"));
} /**
* 生成AsciiDocs格式文档,并汇总成一个文件
* @throws Exception
*/
@Test
public void generateAsciiDocsToFile() throws Exception {
// 输出Ascii到单文件
Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder()
.withMarkupLanguage(MarkupLanguage.ASCIIDOC)
.withOutputLanguage(Language.ZH)
.withPathsGroupedBy(GroupBy.TAGS)
.withGeneratedExamples()
.withoutInlineSchema()
.build(); Swagger2MarkupConverter.from(new URL("http://localhost:8080/v2/api-docs"))
.withConfig(config)
.build()
.toFile(Paths.get("./docs/asciidoc/generated/all"));
} /**
* 生成Markdown格式文档,并汇总成一个文件
* @throws Exception
*/
@Test
public void generateMarkdownDocsToFile() throws Exception {
// 输出Markdown到单文件
Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder()
.withMarkupLanguage(MarkupLanguage.MARKDOWN)
.withOutputLanguage(Language.ZH)
.withPathsGroupedBy(GroupBy.TAGS)
.withGeneratedExamples()
.withoutInlineSchema()
.build(); Swagger2MarkupConverter.from(new URL("http://localhost:8080/v2/api-docs"))
.withConfig(config)
.build()
.toFile(Paths.get("./docs/markdown/generated/all"));
}
  1. 使用maven插件生成HTML文档

     <plugins>
    <plugin>
    <groupId>org.asciidoctor</groupId>
    <artifactId>asciidoctor-maven-plugin</artifactId>
    <version>1.5.6</version>
    <configuration>
    <sourceDirectory>./docs/asciidoc/generated</sourceDirectory>
    <outputDirectory>./docs/asciidoc/html</outputDirectory>
    <headerFooter>true</headerFooter>
    <doctype>book</doctype>
    <backend>html</backend>
    <sourceHighlighter>coderay</sourceHighlighter>
    <attributes>
    <!--菜单栏在左边-->
    <toc>left</toc>
    <!--多标题排列-->
    <toclevels>3</toclevels>
    <!--自动打数字序号-->
    <sectnums>true</sectnums>
    </attributes>
    </configuration>
    </plugin>
    </plugins>

效果

实际项目应用后效果

代码

https://github.com/changdaye/swagger-demo

鸣谢

文章出处: http://blog.didispace.com/swagger2markup-asciidoc/

使用Swagger2Markup归档swagger生成的API文档的更多相关文章

  1. Spring Boot 集成 Swagger 生成 RESTful API 文档

    原文链接: Spring Boot 集成 Swagger 生成 RESTful API 文档 简介 Swagger 官网是这么描述它的:The Best APIs are Built with Swa ...

  2. springmvc使用swagger生成rest api文档

    pom.xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-s ...

  3. Swagger UI及 Swagger editor教程 API文档搭配 Node使用

    swagger ui 是一个在线文档生成和测试的利器,目前发现最好用的.为啥好用呢?打开 demo,支持API自动生成同步的在线文档些文档可用于项目内部API审核方便测试人员了解 API这些文档可作为 ...

  4. Spring Boot学习笔记 - 整合Swagger2自动生成RESTful API文档

    1.添加Swagger2依赖 在pom.xml中加入Swagger2的依赖 <!--swagger2--> <dependency> <groupId>io.spr ...

  5. Swagger UI教程 API 文档神器 搭配Node使用

    ASP.NET Web API 使用Swagger生成在线帮助测试文档 Swagger 生成 ASP.NET Web API 前言 swagger ui是一个API在线文档生成和测试的利器,目前发现最 ...

  6. Golang使用swaggo自动生成Restful API文档

    #关于Swaggo 相信很多程序猿和我一样不喜欢写API文档.写代码多舒服,写文档不仅要花费大量的时间,有时候还不能做到面面具全.但API文档是必不可少的,相信其重要性就不用我说了,一份含糊的文档甚至 ...

  7. GhostDoc:生成.NET API文档的工具 (帮忙文档)

    在 Sandcastle:生成.NET API文档的工具 (帮忙文档) 后提供另一个生成API文档的工具.   1) 准备工作 安装GhostDoc Proc. 收费的哦.... 这个工具的优势是不像 ...

  8. 使用jsdoc-toolkit来自动生成js api文档

    近来前端组小盆友开发的类库越来越多,很多情况下彼此不知道写了些什么方法,为了更好的合作提高工作效率,找了个比较好的api文档生成方法.使用jsdoc-toolkit来自动生成js api文档. 一.  ...

  9. Grunt-jsdoc生成JS API文档

    Grunt-jsdoc生成JS API文档 具体的请看官网 https://github.com/krampstudio/grunt-jsdoc 一:首先确保本机电脑上是否已经安装了nodejs和np ...

随机推荐

  1. join()和split()

    一.join()方法 Python join() 方法用于将序列中的元素以指定的字符连接生成一个新的字符串. 如序列为字典,只连接字典里的键 序列里的元素也需要是字符串,如果不为字符串,则会报错 二. ...

  2. linux kill命令

    代码 elif [ "$SIGNAL" = 'reload' ]; then kill -USR1 $PID 总结:kill -9 pid 等同于kill -USR9 pid 等同 ...

  3. linux下vnstat查看服务器带宽流量统计

      因为很多vps或者服务器都是限流量的,但是又很多服务商并没有提供详细的流量表,比如每天的流量表,所以肯定有人很想知道自己服务器到底跑了多少流量. vnstat就是一个很好用的服务器流量统计命令.我 ...

  4. React的性能优化 - 代码拆分之lazy的使用方法

    我们在某些网站上肯定看到过这样一种现象,页面上图片只有你滚动到那个位置附近的时候才会加载,否则就只占了个位,这就是延迟加载最普遍的应用场景. 我们react框架进行开发的时候也是一样,没有使用的组件是 ...

  5. MyBatis操作数据库(基本增删改查)

    一.准备所需工具(jar包和数据库驱动) 网上搜索下载就可以 二.新建一个Java project 1.将下载好的包导入项目中,build path 2.编写MyBatis配置文件:主要填写prope ...

  6. aarch64 架构 交叉编译 tcpdump

    1. 下载 tcpdump 源码 地址 :http://www.tcpdump.org/    (4.9.2) tcpdump 依赖 libpcap  源码 地址 : http://www.tcpdu ...

  7. 2019南昌邀请赛预选赛 I. Max answer (前缀和+单调栈)

    题目:https://nanti.jisuanke.com/t/38228 这题题解参考网上大佬的. 程序的L[i],R[i]代表a[i]这个点的值在区间 [L[i],R[i]] 中最小的并且能拓展到 ...

  8. 创建win32 dll 空项目

    动态库,多字节 win32 空项目 添加导出头文件  类 导入: #pragma once #ifndef IP_CLASS_DLL_H #define IP_CLASS_DLL_H #pragma ...

  9. 使用Gradle发布项目到JCenter仓库 (转载)

    原文:使用Gradle发布项目到JCenter仓库 这篇文章介绍通过Gradle把开源项目发布到公共仓库JCenter中,方便你我他的事情,我们都是很懒的嘛.JCenter现在是Android Stu ...

  10. VMware下Ubuntu全屏显示

      开始是这样的 完了之后应该是这样的 1.点开菜单栏的 虚拟机---------> 安装VMware Tools 安装完了之后桌面会出现一个这样的图标 双击这个DVD,进去之后左侧目录出现了 ...