스프링부트에 Swagger 적용하기

ay.zip·2022년 7월 31일
0

SpringBoot

목록 보기
2/2

1. Swagger (스웨거)

Web API를 자동으로 문서화 해주는 오픈소스 소프트웨어

2. 스프링부트에 적용하기

(1) build.gradle

// Swagger
implementation 'io.springfox:springfox-swagger2:3.0.0'
implementation 'io.springfox:springfox-swagger-ui:3.0.0'
implementation 'io.springfox:springfox-boot-starter:3.0.0'

(2) Configuration

👇 오류난 코드

@Configuration

@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket swagger() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }
}

다른 블로그에서 하라는 대로 작성해서 실행하고
http://localhost:8080/swagger-ui/index.html

에 접속하니 Whitelabel Error Page 가 떴다.

로그에는 No mapping for GET /swagger-ui/index.html 가 떴고.

열심히 블로그를 뒤진 결과 다음과 같이 수정하라는 글을 볼 수 있었다.

👇 성공한 코드 (출처 : https://pipe0502.tistory.com/entry/swagger2-Whitelabel-Error-Page )

@Configuration
@EnableSwagger2
@EnableAsync
@EnableWebMvc
public class SwaggerConfig implements WebMvcConfigurer {

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

    @Bean
    public Docket swagger() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }
}

다시 http://localhost:8080/swagger-ui/index.html#/ 여기로 접속해보니 이젠 제대로 작동한다.

0개의 댓글