這篇文章將帶你了解如何用spring官方推薦的restdoc去生成api文檔。本文創(chuàng)建一個簡單的springboot工程,將http接口通過Api文檔暴露出來。只需要通過 JUnit單元測試和Spring的MockMVC就可以生成文檔。
樟樹ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)建站的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18982081108(備注:SSL證書合作)期待與您的合作!
準備工作
創(chuàng)建工程
引入依賴,其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通常創(chuàng)建一個controller:
@RestController
public class HomeController {
@GetMapping("/")
public Map<String, Object> greeting() {
return Collections.singletonMap("message", "Hello World");
}
}
啟動工程,訪問localhost:8080,瀏覽器顯示:
{“message”:”Hello World”}證明接口已經(jīng)寫好了,但是如何通過restdoc生存api文檔呢
Restdoc,通過單元測試生成api文檔
restdocs是通過單元測試生存snippets文件,然后snippets根據(jù)插件生成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文件,并指定了存放位置。
啟動單元測試,測試通過,你會發(fā)現(xiàn)在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
創(chuàng)建一個新文件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文件你可以通網(wǎng)站發(fā)布出去。整個過程很簡單,對代碼無任何影響。
源碼下載:https://github.com/forezp/SpringBootLearning
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
當前題目:詳解如何用springRestdocs創(chuàng)建API文檔
地址分享:http://www.chinadenli.net/article40/gcsgeo.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供Google、全網(wǎng)營銷推廣、網(wǎng)站設計公司、自適應網(wǎng)站、企業(yè)網(wǎng)站制作、移動網(wǎng)站建設
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)