這篇文章將帶你了解如何用spring官方推薦的restdoc去生成api文檔。本文創建一個簡單的springboot工程,將http接口通過Api文檔暴露出來。只需要通過 JUnit單元測試和Spring的MockMVC就可以生成文檔。
準備工作
- 你需要15min
- Jdk 1.8
- maven 3.0+
- idea
創建工程
引入依賴,其pom文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
< 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
1
2
3
4
5
6
7
|
@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application. class , args); } } |
在springboot通常創建一個controller:
1
2
3
4
5
6
7
8
9
|
@RestController public class HomeController { @GetMapping ( "/" ) public Map<String, Object> greeting() { return Collections.singletonMap( "message" , "Hello World" ); } } |
啟動工程,訪問localhost:8080,瀏覽器顯示:
1
|
{“message”:”Hello World”} |
證明接口已經寫好了,但是如何通過restdoc生存api文檔呢
Restdoc,通過單元測試生成api文檔
restdocs是通過單元測試生存snippets文件,然后snippets根據插件生成htm文檔的。
建一個單元測試類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
@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文件,并指定了存放位置。
啟動單元測試,測試通過,你會發現在target文件下生成了一個snippets文件夾,其目錄結構如下:
1
2
3
4
5
6
7
|
└── 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
創建一個新文件src/main/asciidoc/index.adoc :
1
2
3
4
5
6
7
8
9
10
11
|
用 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文件加上:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
< 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文件你可以通網站發布出去。整個過程很簡單,對代碼無任何影響。
源碼下載:https://github.com/forezp/SpringBootLearning
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/forezp/article/details/71023510