|
@@ -0,0 +1,64 @@
|
|
|
+package com.diagbot;
|
|
|
+
|
|
|
+import org.junit.Test;
|
|
|
+import org.junit.runner.RunWith;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs;
|
|
|
+import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
|
|
+import org.springframework.boot.test.context.SpringBootTest;
|
|
|
+import org.springframework.http.MediaType;
|
|
|
+import org.springframework.mock.web.MockHttpServletResponse;
|
|
|
+import org.springframework.test.context.junit4.SpringRunner;
|
|
|
+import org.springframework.test.context.web.WebAppConfiguration;
|
|
|
+import org.springframework.test.web.servlet.MockMvc;
|
|
|
+import org.springframework.test.web.servlet.MvcResult;
|
|
|
+
|
|
|
+import java.io.BufferedWriter;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.nio.file.Paths;
|
|
|
+
|
|
|
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
|
|
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Description:
|
|
|
+ * @author: gaodm
|
|
|
+ * @time: 2019/7/23 9:22
|
|
|
+ */
|
|
|
+@WebAppConfiguration
|
|
|
+@RunWith(SpringRunner.class)
|
|
|
+@AutoConfigureRestDocs(outputDir = "build/asciidoc/snippets")
|
|
|
+@SpringBootTest
|
|
|
+@AutoConfigureMockMvc
|
|
|
+public class Swagger2MarkupTest {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private MockMvc mockMvc;
|
|
|
+
|
|
|
+// @Test
|
|
|
+// public void testApi() throws Exception {
|
|
|
+// mockMvc.perform(get("/greeting")
|
|
|
+// .accept(MediaType.APPLICATION_JSON))
|
|
|
+// .andDo(document("greetingGet",
|
|
|
+// Preprocessors.preprocessResponse(Preprocessors.prettyPrint())))
|
|
|
+// .andExpect(status().isOk());
|
|
|
+// }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void createSpringfoxSwaggerJson() throws Exception {
|
|
|
+
|
|
|
+ String outputDir = System.getProperty("io.springfox.staticdocs.outputDir");
|
|
|
+ MvcResult mvcResult = this.mockMvc.perform(get("/v2/api-docs")
|
|
|
+ .accept(MediaType.APPLICATION_JSON))
|
|
|
+ .andExpect(status().isOk())
|
|
|
+ .andReturn();
|
|
|
+
|
|
|
+ MockHttpServletResponse response = mvcResult.getResponse();
|
|
|
+ String swaggerJson = response.getContentAsString();
|
|
|
+ Files.createDirectories(Paths.get(outputDir));
|
|
|
+ try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(outputDir, "swagger.json"), StandardCharsets.UTF_8)) {
|
|
|
+ writer.write(swaggerJson);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|