引言
在學(xué)會了使用 Swagger 之后,我們能夠輕松地為 SpringBoot 項目自動構(gòu)建API文檔。但是,我們構(gòu)建的文檔需要在項目中整合 swagger-ui 或使用單獨部署的 swagger-ui 和 /v2/api-docs 返回的配置信息才能展現(xiàn)出 API 文檔。這里我們將在使用 Swagger 的基礎(chǔ)上,介紹一種生成靜態(tài) API 文檔的方法,以便于構(gòu)建更輕量部署和使用的 API 文檔。
Swagger2Markup
Swagger2Markup 是 Github 上的開源項目。Swagger2Markup 可以將 Swagger 生成的文檔轉(zhuǎn)化成流行的格式以便于獨立部署和使用,如:Markdown、Confluence、AsciiDoc。
GitHub:https://github.com/Swagger2Markup/swagger2markup
生成AsciiDoc(Java代碼生成)
一、編輯 pom.xml 增加需要使用的相關(guān)依賴、倉庫和插件
<dependency>
<groupId>io.github.swagger2markup</groupId>
<artifactId>swagger2markup</artifactId>
<version>1.3.1</version>
</dependency>
<!-- 引用以下兩個包是防止生成過程中出現(xiàn)找不到類的異常 -->
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-core</artifactId>
<version>1.5.16</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
<version>1.5.16</version>
</dependency>
<!--測試依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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>
<build>
<plugins>
<plugin>
<groupId>org.asciidoctor</groupId>
<artifactId>asciidoctor-maven-plugin</artifactId>
<version>1.5.6</version>
<configuration>
<!-- asciidoc文件目錄 -->
<sourceDirectory>./docs</sourceDirectory>
<!-- 生成HTML的路徑 -->
<outputDirectory>./html</outputDirectory>
<backend>html</backend>
<sourceHighlighter>coderay</sourceHighlighter>
<attributes>
<!-- 導(dǎo)航欄在左側(cè) -->
<toc>left</toc>
<!-- 顯示菜單層級數(shù) -->
<toclevels>3</toclevels>
<!-- 自動打數(shù)字序號 -->
<sectnums>ture</sectnums>
</attributes>
</configuration>
</plugin>
</plugins>
</build>
二、編寫一個單元測試用例來生成執(zhí)行生成文檔的代碼
import java.net.URL;
import java.nio.file.Paths;
import org.junit.Test;
import io.github.swagger2markup.GroupBy;
import io.github.swagger2markup.Language;
import io.github.swagger2markup.Swagger2MarkupConfig;
import io.github.swagger2markup.Swagger2MarkupConverter;
import io.github.swagger2markup.builder.Swagger2MarkupConfigBuilder;
import io.github.swagger2markup.markup.builder.MarkupLanguage;
public class Swagger2MarkupTest {
/**
* 生成AsciiDocs格式文檔
*
* @throws Exception
*/
@Test
public void generateAsciiDocs() 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()
.toFolder(Paths.get("./docs/asciidoc/generated"));
}
/**
* 生成Markdown格式文檔
*
* @throws Exception
*/
@Test
public void generateMarkdownDocs() 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()
.toFolder(Paths.get("./docs/markdown/generated"));
}
/**
* 生成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"));
}
}
以上代碼說明了幾個關(guān)鍵內(nèi)容:
MarkupLanguage.ASCIIDOC:指定了要輸出的最終格式。除了ASCIIDOC之外,還有MARKDOWN和CONFLUENCE_MARKUP。
from(new URL("http://localhost:8080/v2/api-docs"):指定了生成靜態(tài)文檔的數(shù)據(jù)源配置。
toFolder(Paths.get("src/docs/asciidoc/generated"):指定了最終生成文件的目錄位置。如果不想分割結(jié)果文件,也可以改成toFile(Paths.get("src/docs/asciidoc/generated/all")),將轉(zhuǎn)換結(jié)果輸出到一個文件中,這樣最終也只會生成一個html文件。
在執(zhí)行測試用例之后【執(zhí)行前需要運行主服務(wù),也就是接口服務(wù)】,就能在當(dāng)前項目的目錄下獲得如下內(nèi)容:

三、通過cmd進(jìn)入到項目的pom.xml目錄下執(zhí)行以下命令
mvn asciidoctor:process-asciidoc
命令執(zhí)行完成即生成HTML文檔,文件結(jié)構(gòu)如下:

四、文檔樣例如下:



