Springboot에 Swagger v3 연동하기

Yuri JI·2022년 9월 20일
0
  • 버전
    • Springboot 2.6.11
    • Java 8
    • Swagger v3

📌 1. 의존성 추가 (build.gradle)

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

📌2. config/swager/SwaggerUiWebMvcConfigurer.class 작성

**

import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

public class SwaggerUiWebMvcConfigurer extends WebMvcConfigurationSupport {
    private final String baseUrl;

    public SwaggerUiWebMvcConfigurer(String baseUrl) {
        this.baseUrl = baseUrl;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/swagger-ui/**").addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/");
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController(baseUrl + "/swagger-ui/")
                .setViewName("forward:" + baseUrl + "/swagger-ui/index.html");
    }
}

📌 3. ⭐ application.yml 작성

springboot 2.6.x 버전이라면 해당 코드가 있어야 swagger v3를 적용가능

spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

참고한 블로그

https://minutemaid.tistory.com/m/122

profile
안녕하세요 😄

0개의 댓글