본문 바로가기
개발/java,spring,springboot

spring fox 적용하기(springboot) - 자동문서화

by 개발자종혁 2020. 12. 17.
728x90

Spring fox란?

- spring과 연동하는 자동 문서화 라이브러리

 

1. Springfox 관련 설정

1. gradle 설정

repositories {
     …
    maven { url 'http://oss.jfrog.org/artifactory/oss-snapshot-local/' }

}
dependencies {
    implementation "io.springfox:springfox-boot-starter:3.0.0"
    implementation "io.springfox:springfox-swagger-ui:3.0.0"
      ...
}

2. Config 파일

@Configuration
public class SpringFoxConfig {
    @Bean public Docket api() {

        return new Docket(DocumentationType.OAS_30).select()
                                                   .apis(RequestHandlerSelectors.any())
                                                   .paths(PathSelectors.any()).build();
    }
}

 

2. 접속

 

3. Spring security 사용시 설정

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Override
    protected void configure(HttpSecurity http) throws Exception {


          http.authorizeRequests()  
                .antMatchers("/v3/api-docs/**").permitAll()   // OAS_30
                .antMatchers("/swagger-resources/**").permitAll()
                // .antMatchers("/v2/api-docs/**").permitAll()   // swagger 2
                .antMatchers("/swagger-ui/**").permitAll();

    }

}

 

4. 새로운 스타일을 적용하고 싶은 경우 (예시: redoc )

redoc

 

Redocly/redoc

📘 OpenAPI/Swagger-generated API Reference Documentation - Redocly/redoc

github.com

1. 설정

  1. spring boot 경로 : /resources/dist/redoc/index.html
<!DOCTYPE html> 
<html> 
  <head> 
    <title>ReDoc</title> 
    <!-- needed for adaptive design --> 
    <meta charset="utf-8" /> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link href="https://fonts.googleapis.com/css?family=Montserrat:300,400,700|Roboto:300,400,700" rel="stylesheet"> 
    <!-- ReDoc doesn't change outer page styles --> 
    <style> body { margin: 0; padding: 0; } </style> 
  </head> 
  <body> 
    <div id="redoc-container"></div> 
    <script src="/dist/redoc/redoc.standalone.js"> </script> 
    <script> 
      window.onload = function () { 
        var url = window.location.origin + "/v3/api-docs"; 
        Redoc.init(url, { scrollYOffset: 50 }, document.getElementById('redoc-container')) 
      } 
    </script> 
  </body> 
</html>

2. 리소스 핸들러추가

@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {
      @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/*.worker.js").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/js/**").addResourceLocations("classpath:/static/js/");
        registry.addResourceHandler("/fonts/**").addResourceLocations("classpath:/static/fonts/");
        registry.addResourceHandler("/css/**").addResourceLocations("classpath:/static/css/");
        registry.addResourceHandler("/favicon.ico").addResourceLocations("classpath:/");
        // index.html 파일 경로
        registry.addResourceHandler("/dist/**").addResourceLocations("classpath:/dist/");

    }
}

3. 접속 http://localhost:8080이 base url이면, http://localhost:8080/dist/redoc/index.html로 접속

 

5. 기타

- 세부사항 적용 https://dimitr.im/documenting-rest-api-swagger-springfox

 

Documenting your REST API with Swagger and Springfox

REST services are pretty great to allow reuse of your operations, however, to become reusable, you'll have to properly document the available endpoints in your REST API. A popular standard, that is used for this, is Swagger.

dimitr.im

 

 

728x90

댓글