BackEnd/Spring

[SpringBoot] [트러블슈팅] Swagger2 - ApplicationContextException: failed to start bean 'documentationpluginsbootstrapper'

PgmJUN 2022. 1. 18. 00:49

 

 

 

ApplicationContextException: failed to start bean 'documentationpluginsbootstrapper'

 

API Spec 자동 문서화를 위해 SpringFox-Swagger2 를 사용할 때

 

failed to start bean 'documentationpluginsbootstrapper';

이라는 오류 메세지와 함께 ApplicationContextException 예외가 발생하였다. 

 

 

원인을 찾아보니 스프링 부트의 버전과 Swagger2의 버전이 호환되지 않아서 발생하는 문제였다.

 

Spring boot 2.6버전 이후에 spring.mvc.pathmatch.matching-strategy 값이

 

ant_apth_matcher에서 path_pattern_parser변경되면서 몇몇 라이브러리(swagger포함)에 오류가 발생한다고 한다.

 

 

이 문제를 해결하기 위해 여러 방안을 몰색했고, 해결 방법을 3가지를 찾았다.

 

 

 


해결방법(1)

 

첫 번째로 해결할 수 있는 방법이다.

 

application.properties

# Swagger "org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException" Error resolve
spring.mvc.pathmatch.matching-strategy = ANT_PATH_MATCHER

 

application.properties 에 해당 내용을 복사해서 붙여넣어준다.

 

 


해결방법(2)

 

두 번째 해결 방법이다.

 

application.yml

spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

 

application.yml 에 해당 내용을 복사해서 붙여넣어준다.

 

 


해결방법(3)

 

위의 방법도 해결 방법 중 하나지만, 내 경험으론 방법(1)은 Maven 프로젝트에서 통했고,

 

방법(2)로는 해결이 되지 않았다.

 

gradle 프로젝트로 진행 중인 동아리 DND의 '걱정 기록 서비스' 에서는 지금 이 방법으로 해결했다.

 

 

보통 Swagger2 를 사용하기 위해선

@Configuration, @EnableSwagger2 어노테이션을 사용하여 아래와 같은 환경 설정 클래스를 만든다.

 

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    private static final String API_NAME = "Test API";
    private static final String API_VERSION = "0.0.1";
    private static final String API_DESCRIPTION = "Test API 명세서";

    @Bean
    public Docket api() {

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.abc.bcd"))
                .paths(PathSelectors.any())
                .build();
    }

    public ApiInfo apiInfo() {
	return new ApiInfoBuilder()
		.title(API_NAME)
		.version(API_VERSION)
		.description(API_DESCRIPTION)
		.build();
    }
}

 

하지만 위와 같이 설정하면 아래와 같은 리소스를 등록하는 설정을 하지 않아 이러한 현상이 발생한다고 들었다.

 

그래서 SwaggerConfig를 아래와 같이 설정하여 문제를 해결할 수 있었다.

 

@Configuration
@EnableAsync
@EnableWebMvc
@EnableSwagger2
public class SwaggerConfig implements WebMvcConfigurer {
    private static final String API_NAME = "Test API";
    private static final String API_VERSION = "0.0.1";
    private static final String API_DESCRIPTION = "Test API 명세서";

    @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)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.abc.bcd"))
                .paths(PathSelectors.any())
                .build(); }

    public ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(API_NAME)
                .version(API_VERSION)
                .description(API_DESCRIPTION)
                .build();
    }

}

 

 

 

 

 

 

 

 

 

 

 

 

reference:

https://kim-jong-hyun.tistory.com/49

https://pipe0502.tistory.com/entry/swagger2-Whitelabel-Error-Page

https://www.inflearn.com/questions/230160

728x90
반응형