本篇文章為大家展示了如何用spring Restdocs創建API文檔,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

交城網站建設公司成都創新互聯公司,交城網站設計制作,有大型網站制作公司豐富經驗。已為交城近1000家提供企業網站建設服務。企業網站搭建\外貿網站建設要多少錢,請找那個售后服務好的交城做網站的公司定做!
你需要15min
Jdk 1.8
maven 3.0+
idea
引入依賴,其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通常創建一個controller:
@RestController
public class HomeController {
@GetMapping("/")
public Map<String, Object> greeting() {
return Collections.singletonMap("message", "Hello World");
}
}啟動工程,訪問localhost:8080,瀏覽器顯示:
{“message”:”Hello World”}
證明接口已經寫好了,但是如何通過restdoc生存api文檔呢
restdocs是通過單元測試生存snippets文件,然后snippets根據插件生成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文件,并指定了存放位置。
啟動單元測試,測試通過,你會發現在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文件生成文檔。
創建一個新文件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文件你可以通網站發布出去。整個過程很簡單,對代碼無任何影響。
源碼下載:https://github.com/forezp/SpringBootLearning
上述內容就是如何用spring Restdocs創建API文檔,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注創新互聯行業資訊頻道。
網站題目:如何用springRestdocs創建API文檔
本文URL:http://www.yijiale78.com/article6/pehpog.html
成都網站建設公司_創新互聯,為您提供標簽優化、全網營銷推廣、品牌網站建設、App開發、企業建站、微信公眾號
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯