Spring

Spring Actuator 와 Swagger를 사용할 시 의존성 오류

웅둘 2023. 10. 26. 13:29

Spring Swagger가 적용되어 있는 환경 속에 모니터링 시스템을 위해 Actuator를 추가하였더니

아래와 같은 오류가 발생했다.

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

기본적으로 해당 오류는 Swagger에서 발생하는 오류로 spring fox에서 업데이트를 진행하지 않아 생긴 버전 이슈이다.

application.yml

spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

Actuator를 사용하지 않는다면 해당 설정으로 오류를 해결할 수 있다.

하지만 Swagger와 Actuator를 동시에 사용한다면 해당 설정으로 해결되지 않는다.

 

문제 원인

  • Swagger는 모든 endpoint에 대해 문서화를 해주는 기능이다.
  • Actuator 또한 endpoint 등을 직접 생성하는 역할 → Swagger와 의존성이 충돌한다.
@Configuration
public class ServerConfig {
	
@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));
	}
}
  • 해당 코드는 Spring 웹 애플리케이션에서 엔드포인트를 설정하는 것입니다.
  • webEndpointsSupplier, servletEndpointsSupplier, controllerEndpointsSupplier 해당 endpoint 정보를 가져옵니다.
  • basePath를 기반으로 하여 EndpointMapping 객체를 생성하여 경로를 설정합니다.
  • shouldRegisterLinksMapping : endpoint의 디스커버리와 포트를 링크 매핑에 등록할지 여부를 결정합니다.
  • **디스커버리 엔드포인트(Discovery Endpoint)**는 Spring Boot 애플리케이션에서 사용 가능한 다양한 서비스 및 엔드포인트를 찾고 검색하기 위한 엔드포인트 중 하나입니다.