SpringBoot actuator와 Swagger를 같이 쓸때 (org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException:) 오류 해결법

코딩을 합시다·2023년 2월 25일
0

스프링 부트에 actuator를 build.gradle에 추가하고 app을 돌려봤더니

implementation("org.springframework.boot:spring-boot-starter-actuator")

Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException:

이런 오류가 났다
이건 분명 스웨거 오류인데??
spring fox에서 업데이트를 안 해서 생긴 버전 이슈로 아래 코드를 설정해주면 된다.

일단
application.yml 또는 application.properties에 아래 구문을 추가해준다.

spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

그리고 스웨거Config에 아래 구문을 추가해준다.
Bean이므로 어디에 추가해도 상관없을 것으로 생각하나 본인은 SwaggerConfig를 관리하는 클래스에 추가하였다.

Swagger.config

import org.springframework.core.env.Environment;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.CorsEndpointProperties;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementPortType;
import org.springframework.boot.actuate.endpoint.ExposableEndpoint;
import org.springframework.boot.actuate.endpoint.web.*;
import org.springframework.boot.actuate.endpoint.web.annotation.ControllerEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.annotation.ServletEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping;

@Configuration
@EnableSwagger2
@Import(BeanValidatorPluginsConfiguration.class)
public class SwaggerConfig {
	
    // 중략

    @Bean
    public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(WebEndpointsSupplier webEndpointsSupplier
            , ServletEndpointsSupplier servletEndpointsSupplier
            , ControllerEndpointsSupplier controllerEndpointsSupplier
            , EndpointMediaTypes endpointMediaTypes
            , CorsEndpointProperties corsProperties
            , WebEndpointProperties webEndpointProperties
            , Environment environment) {
        List<ExposableEndpoint<?>> allEndpoints = new ArrayList();
        Collection<ExposableWebEndpoint> webEndpoints = webEndpointsSupplier.getEndpoints();
        allEndpoints.addAll(webEndpoints);
        allEndpoints.addAll(servletEndpointsSupplier.getEndpoints());
        allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());
        String basePath = webEndpointProperties.getBasePath();
        EndpointMapping endpointMapping = new EndpointMapping(basePath);
        boolean shouldRegisterLinksMapping = this.shouldRegisterLinksMapping(webEndpointProperties, environment, basePath);
        return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints, endpointMediaTypes, corsProperties.toCorsConfiguration()
                , new EndpointLinksResolver(allEndpoints, basePath), shouldRegisterLinksMapping, null);
    }


    private boolean shouldRegisterLinksMapping(WebEndpointProperties webEndpointProperties, Environment environment, String basePath) {
        return webEndpointProperties.getDiscovery().isEnabled() && (StringUtils.hasText(basePath) || ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT));
    }

    // 중략
}
  1. WebMvcEndpointHandlerMapping 이 클래스는 스프링 웹 MVC 프레임워크에서 사용되는 URL 경로와 Actuator 엔드포인트를 매핑하는 핸들러 매핑 클래스이다.
  2. webEndpointServletHandlerMapping 메소드는 WebEndpointsSupplier, ServletEndpointsSupplier, ControllerEndpointsSupplier 등의 객체를 파라미터로 받는데 이들은 각각 Spring Boot 애플리케이션에서 사용할 수 있는 엔드포인트들을 제공하는 객체입니다.
  3. URL 경로를 설정하기 위해 WebEndpointProperties 객체를 참조
  4. shouldRegisterLinksMapping() 메소드에서 Actuator 엔드포인트와 관련된 링크 정보를 노출하는 /actuator 링크를 등록할지 여부를 결정

잘실행된다.

0개의 댓글