본문 바로가기
  • 주니어 개발자의
    RESTful 성장기
트러블 슈팅이라 쓰고 뻘짓이라 읽는다.

Spring Boot에 Swagger2 적용하기

by 돌건 2022. 1. 19.

오늘의 Trouble

Spring Boot 프로젝트에 Swagger2를 적용하는 과정에서 "Failed to start bean 'documentationPluginsBootstrapper'"라는 에러를 마주함.

 

원인은 무엇인가?

Spring Boot의 버전이 2.6으로 올라가면서 spring.mvc.pathmatch.matching-strategy의 default 값이 기존 ant_path_matcher에서 path_pattern_parser로 변경되었다고 한다.

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket restAPI() {
        return new Docket( DocumentationType.SWAGGER_2 )
                .apiInfo( apiInfo() )
                .select()
                .apis( RequestHandlerSelectors.basePackage( "com.todo.api" ) )
                .paths( PathSelectors.any() )
                .build();
    }

    private ApiInfo apiInfo() {

        return new ApiInfoBuilder()
                .title( "Donggun's Todo REST API" )
                .version( "1.0.0" )
                .description( "Swagger 테스트 중입니다." )
                .build();
    }
}​

위 코드에서 PathSelectors.any()를 통해서 모든 요청 경로에 대해 API 문서를 생성하려고 하는데서 문제가 된 거 같다. 아무리 구글링을 해봐도 정확한 이유는 나오지 않고, 단지 springfox의 plugin은 Path pattern과 적합하지 않고, Ant path pattern을 이용해야 한다고 한다.. 그래서 AntPathMatcher와 PathPattern을 직접 docs.spring.io에서 찾아봤다. 

PathPattern의 경우 AntPathMatcher에 비해서 많은 URL 매핑 규칙을 지니고 있으며, ** 을 사용하는 규칙이 다를 뿐이다..! 뭔가 속 시원하지 않지만 결론은, URL 경로를 매핑하는 데 있어 어떤 전략을 사용하느냐의 문제였다.

 

해결

application.yml에 

spring:
    mvc:
        pathmatch:
            matching-strategy: ant_path_matcher

을 추가해줬다

댓글