這篇文章將帶你了解如何用spring官方推薦的restdoc去生成api文檔。本文創(chuàng)建一個(gè)簡單的springboot工程,將http接口通過Api文檔暴露出來。只需要通過 JUnit單元測(cè)試和Spring的MockMVC就可以生成文檔。
樟樹ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)建站的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18982081108(備注:SSL證書合作)期待與您的合作!
準(zhǔn)備工作
創(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)建一個(gè)controller:
@RestController public class HomeController { @GetMapping("/") public Map<String, Object> greeting() { return Collections.singletonMap("message", "Hello World"); } }
啟動(dòng)工程,訪問localhost:8080,瀏覽器顯示:
{“message”:”Hello World”}
證明接口已經(jīng)寫好了,但是如何通過restdoc生存api文檔呢
Restdoc,通過單元測(cè)試生成api文檔
restdocs是通過單元測(cè)試生存snippets文件,然后snippets根據(jù)插件生成htm文檔的。
建一個(gè)單元測(cè)試類:
@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文件,并指定了存放位置。
啟動(dòng)單元測(cè)試,測(cè)試通過,你會(huì)發(fā)現(xiàn)在target文件下生成了一個(gè)snippets文件夾,其目錄結(jié)構(gòu)如下:
└── target └── snippets └── home └── httpie-request.adoc └── curl-request.adoc └── http-request.adoc └── http-response.adoc
默認(rèn)情況下,snippets是Asciidoctor格式的文件,包括request和reponse,另外其他兩種httpie和curl兩種流行的命令行的http請(qǐng)求模式。
到目前為止,只生成了Snippets文件,需要用Snippets文件生成文檔。
怎么用Snippets
創(chuàng)建一個(gè)新文件src/main/asciidoc/index.adoc :
用 Spring REST Docs 構(gòu)建文檔 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[] 這個(gè)例子非常簡單,通過單元測(cè)試和一些簡單的配置就能夠得到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>
這時(shí)只需要通過mvnw package命令就可以生成文檔了。
在/target/generated-docs下有個(gè)index.html,打開這個(gè)html,顯示如下,界面還算簡潔:
結(jié)語
通過單元測(cè)試,生存adoc文件,再用adoc文件生存html,只需要簡單的幾步就可以生成一個(gè)api文檔的html文件,這個(gè)html文件你可以通網(wǎng)站發(fā)布出去。整個(gè)過程很簡單,對(duì)代碼無任何影響。
源碼下載:https://github.com/forezp/SpringBootLearning
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
當(dāng)前題目:詳解如何用springRestdocs創(chuàng)建API文檔
地址分享:http://m.newbst.com/article40/gcsgeo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、全網(wǎng)營銷推廣、網(wǎng)站設(shè)計(jì)公司、自適應(yīng)網(wǎng)站、企業(yè)網(wǎng)站制作、移動(dòng)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)