문서의 선택한 두 판 사이의 차이를 보여줍니다.
양쪽 이전 판 이전 판 다음 판 | 이전 판 | ||
programming:documentation:swagger [2018/07/18 17:00] kwon37xi |
programming:documentation:swagger [2023/08/17 15:39] (현재) kwon37xi [ReDoc] |
||
---|---|---|---|
줄 1: | 줄 1: | ||
====== Swagger ====== | ====== Swagger ====== | ||
* http:// | * http:// | ||
+ | * [[programming: | ||
+ | * -> [[programming: | ||
+ | * [[springframework: | ||
* [[https:// | * [[https:// | ||
* [[https:// | * [[https:// | ||
- | ===== Spring MVC and swagger | + | ===== ReDoc ===== |
- | * [[https:// | + | * [[https:// |
- | * [[https:// | + | * [[https://webhookrelay.com/blog/2018/11/05/openapi-redoc-tutorial/|Documenting your API with OpenAPI (Swagger) and Redoc — Web Relay]] |
- | * Spring MVC Swagger 튜토리얼 시리즈 | + | * 페이지 내 검색과 더 유려한 UI를 제공해줌. |
- | * [[http:// | + | |
- | * [[http://www.javacodegeeks.com/2014/11/spring-rest-api-with-swagger-exposing-documentation.html|Spring Rest API with Swagger | + | |
- | * Spring MVC API 적용 Servlet의 "/" | + | |
- | // /css/*, /js/* 등에 대한 핸들러 추가. | + | |
- | @Override | + | |
- | protected void addResourceHandlers(ResourceHandlerRegistry registry) { | + | |
- | registry.addResourceHandler(" | + | |
- | .addResourceLocations(WEB_JAR_RESOURCE_LOCATION, | + | |
- | WEB_JAR_RESOURCE_LOCATION + " | + | |
- | WEB_JAR_RESOURCE_LOCATION + " | + | |
- | WEB_JAR_RESOURCE_LOCATION + " | + | |
- | WEB_JAR_RESOURCE_LOCATION + " | + | |
- | .setCachePeriod(0); | + | |
- | super.addResourceHandlers(registry); | + | |
- | } | + | |
- | @ApiIgnore | ||
- | @Controller | ||
- | public class SwaggerController { | ||
- | @Autowired | ||
- | private ResourceLoader resourceLoader; | ||
- | @RequestMapping(" | + | ===== Swagger2MarkUp ===== |
- | | + | * [[https:// |
- | final Resource swaggerHtmlResource = | + | * [[https:// |
- | resourceLoader.getResource(" | + | * [[https:// |
+ | * [[https:// | ||
+ | <code java> | ||
+ | // Spring / Swagger 에서 swagger.json 을 파일로 뽑아내는 예제 | ||
- | final String swaggerHtmlContents = | + | @WebAppConfiguration |
- | | + | @RunWith(SpringJUnit4ClassRunner.class) |
- | final String refinedHtml | + | @AutoConfigureRestDocs(outputDir = "build/ |
- | | + | @SpringBootTest(classes |
+ | @AutoConfigureMockMvc | ||
+ | public class Swagger2MarkupTest { | ||
- | response.setContentType(" | + | private static |
- | | + | |
- | writer.write(refinedHtml); | + | |
- | writer.close(); | + | |
- | } | + | |
- | } | + | |
- | </ | + | |
- | ===== SpringBoot ===== | ||
- | * [[http:// | ||
+ | @Autowired | ||
+ | private MockMvc mockMvc; | ||
+ | ... | ||
+ | |||
+ | @Test | ||
+ | public void createSpringfoxSwaggerJson() throws Exception { | ||
+ | //String designFirstSwaggerLocation = Swagger2MarkupTest.class.getResource("/ | ||
+ | |||
+ | String outputDir = System.getProperty(" | ||
+ | MvcResult mvcResult = this.mockMvc.perform(get("/ | ||
+ | .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, | ||
+ | writer.write(swaggerJson); | ||
+ | } | ||
+ | } | ||
+ | ... | ||
+ | } | ||
+ | </ | ||
+ | ===== 기타 ===== | ||
+ | * https:// | ||
+ | * [[https:// |