사용자 도구

사이트 도구


springframework:springfox

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

다음 판
이전 판
springframework:springfox [2016/06/22 22:55]
kwon37xi 만듦
springframework:springfox [2020/08/26 10:32] (현재)
kwon37xi
줄 1: 줄 1:
 ====== SpringFox ====== ====== SpringFox ======
   * https://springfox.github.io/springfox/docs/current/   * https://springfox.github.io/springfox/docs/current/
-  * [[programming:documentation:swagger|Swagger]] 처럼 Spring Controller를 문서화 해줌+  * [[programming:documentation:swagger|Swagger]] 의 Spring 구현 
 +  * Swagger -> OpenAPI Specification 이 되었으며, OpenAPI Specification 문서화는  [[springframework:springdoc|SpringDoc - SpringFramework Open API Specification]] 참조. 
 +  
  
 +> The Springfox suite of java libraries are all about automating the generation of machine and human readable specifications for JSON APIs written using the spring family of projects. 
  
 +
 +===== Spring MVC and swagger =====
 +  * [[https://github.com/martypitt/swagger-springmvc|Swagger SpringMVC]]
 +  * [[https://github.com/adrianbk/swagger-springmvc-demo/tree/master/swagger-ui|Swagger-UI]]
 +  * Spring MVC Swagger 튜토리얼 시리즈
 +    * [[http://www.javacodegeeks.com/2014/10/spring-rest-api-with-swagger-creating-documentation.html|Spring Rest API with Swagger – Creating documentation Swagger 문서 생성]]
 +    * [[http://www.javacodegeeks.com/2014/11/spring-rest-api-with-swagger-exposing-documentation.html|Spring Rest API with Swagger – Exposing documentation]] Spring REST API를 웹 서비스로 볼 수 있게 해줌
 +  * <del>Spring MVC API 적용 Servlet의 "/"가 아닌 "/api/*" 형태로 매핑되면 Swagger 적용이 복잡해진다.</del> 최근 버전은 매우 간결해짐.<code java>
 +// /css/*, /js/* 등에 대한 핸들러 추가.
 +@Override
 +protected void addResourceHandlers(ResourceHandlerRegistry registry) {
 +  registry.addResourceHandler("css/**", "js/**", "lib/**", "images/**", "swagger-ui.js")
 +    .addResourceLocations(WEB_JAR_RESOURCE_LOCATION,
 +        WEB_JAR_RESOURCE_LOCATION + "css/",
 +        WEB_JAR_RESOURCE_LOCATION + "js/",
 +        WEB_JAR_RESOURCE_LOCATION + "lib/",
 +        WEB_JAR_RESOURCE_LOCATION + "images/")
 +    .setCachePeriod(0);
 +  super.addResourceHandlers(registry);
 +}
 +
 +@ApiIgnore
 +@Controller
 +public class SwaggerController {
 +  @Autowired
 +    private ResourceLoader resourceLoader;
 +
 +  @RequestMapping("/swagger")
 +    public void swagger(HttpServletResponse response) throws IOException {
 +      final Resource swaggerHtmlResource =
 +        resourceLoader.getResource("classpath:/META-INF/resources/sdoc.jsp");
 +
 +      final String swaggerHtmlContents =
 +        CoupangIOUtils.toString(swaggerHtmlResource.getInputStream(), "UTF-8");
 +      final String refinedHtml = swaggerHtmlContents
 +        .replaceAll("http://swagger.wordnik.com", "/api/swagger");
 +
 +      response.setContentType("text/html; charset=UTF-8");
 +      final PrintWriter writer = response.getWriter();
 +      writer.write(refinedHtml);
 +      writer.close();
 +    }
 +}
 +</code>
 +
 +===== 숫자값 parsing 에러 =====
 +  * 파라미터 타입이 primitive 숫자(int, long 등)인데, example을 지정하지 않으면 empty string이 example로 들어오게 되고, 그로인해 Swagger UI 출력시 숫자값 파싱 오류가 발생한다. 이때 example을 적합한 숫자로 넣어주면 된다.
 +  * 원칙적으로 Boxed Type에 대해서는 발생하지 않을거 같고 일부 괜찮은데 일부는 또 파싱 오류가 발생하기도 한다.(example값에 empty string을 넣어서)
 +
 +<code java>
 +@ApiParam(value = "userId", defaultValue="1", example = "1") int userId;
 +</code>
 +
 +===== 참조 =====
 +  * [[http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api|Setting Up Swagger 2 with a Spring REST API ]] [[http://springboot.tistory.com/24|Spring REST API에 Swagger 2 설정하기]]
 +  * [[https://dzone.com/articles/spring-rest-docs-versus-springfox-swagger-for-api|Spring REST Docs Versus SpringFox Swagger for API Documentation - DZone Java]]
 +  * [[https://www.baeldung.com/swagger-2-documentation-for-spring-rest-api|Setting Up Swagger 2 with a Spring REST API | Baeldung]]
 +  * [[https://www.baeldung.com/swagger-ui-turn-off-in-production|How to Turn Off Swagger-ui in Production | Baeldung]]
springframework/springfox.1466605551.txt.gz · 마지막으로 수정됨: 2016/06/22 22:55 저자 kwon37xi