文章出处: 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. 基于MFC的Media Player播放器的制作介绍

    |   版权声明:本文为博主原创文章,未经博主允许不得转载. 因为这次多媒体课程设计做一个基于MFC的播放器,因为本人实力太菜,需要播放音乐或视频文件时候,自己写不出解码 函数,所以准备使用第三方多媒 ...

  2. 现在就去100offer 参加互联网人才拍卖! 现在登录现在注册 为什么整个互联网行业都缺前端工程师?

    现在,几乎整个互联网行业都缺前端工程师,不仅在刚起步的创业公司,上市公司乃至巨头,这个问题也一样存在.没错,优秀的前端工程师简直比大熊猫还稀少. 每天,100offer的HR群都有人在吐槽招不到前端工 ...

  3. 深入了解line-height(各种单位总结1.5/150%/1.5em)

    默认状态,浏览器使用1.0-1.2 line-height, 这是一个初始值.你可以定义line-height属性来覆盖初始值:p{line-height:140%} 你可以有5种方式来定义line- ...

  4. Linux下多线程pthread内存泄露

    目标文件:/proc/<pid>/maps 若其中出现了大量的8K左右的内存碎片,则说明出现了内存泄露.同理,如果相应pid进程的maps文件中出现了很多内存碎片,也说明出现了内存泄露.  ...

  5. css 两边是线,中间文字的多种实现方法

    <div class="soild_text_one"> <fieldset> <legend>历史活动一</legend> < ...

  6. spring基于注解的事务控制

    pom配置: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http ...

  7. Javascript高级程序设计--读书笔记之Array类型

    1.数组的lenght属性 数组的lenght属性很有特点---他不是只读的,可以同过修改这个属性来向数组的末尾添值加或删除值, 删除值 var color = ["red", & ...

  8. OpenCV学习笔记 笔记汇总

    转载来源:https://www.cnblogs.com/tonyc/p/6407318.html 今后开始学习OpenCV   1:OpenCV学习笔记 作者:CSDN数量:55篇博文网址:http ...

  9. tf.placeholde函数解释与用法

    函数原型:tf.placeholder(dtype, shape=None, name=None) 使用说明:该函数用于得到传递进来的真实的训练样本.同时也可以理解为形参, 用于定义过程,在执行的时候 ...

  10. iftop简单使用

    在linux下想查看当前与主机通信的IP有哪些?流量多少?怎么办?使用iftop吧,小巧实用的小工具.在排查问题的时候能起到大作用. centos安装 yum install iftop 界面如下: ...