Swagger2 오류 해결 방법

  • by

스프링 부팅을 공부하면서 Swagger 설정 코드 부분에서 오류가 발생하여 매우 당황했습니다.

첫 번째 오류는

서버 전원이 켜지지 않음

org.springframework.context.ApplicationContextException: Failed to start bean ‘documentationPluginsBootstrapper’; nested exception is java.lang.NullPointerException: Cannot invoke “org.springframework.web.servlet.mvc.condition.Patter of “springfox.documentation.spi.service.did springfox.documentation.RequestHandler)” is null

at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:181) ~(spring-context-5.3.25.jar:5.3.25)

at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:54) ~(spring-context-5.3.25.jar:5.3.25)

at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:356) ~(spring-context-5.3.25.jar:5.3.25)

at java.base/java.lang.Iterable.forEach(Iterable.java:75) ~(na:na)

at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:155) ~(spring-context-5.3.25.jar:5.3.25)

at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:123) ~(spring-context-5.3.25.jar:5.3.25)

at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:935) ~(spring-context-5.3.25.jar:5.3.25)

at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:586) ~(spring-context-5.3.25.jar:5.3.25)

at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147) ~(spring-boot-2.7.9.jar:2.7.9)

at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:731) ~(spring-boot-2.7.9.jar:2.7.9)

at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408) ~(spring-boot-2.7.9.jar:2.7.9)

at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) ~(spring-boot-2.7.9.jar:2.7.9)

at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303) ~(spring-boot-2.7.9.jar:2.7.9)

at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292) ~(spring-boot-2.7.9.jar:2.7.9)

at com.springboot.api.ApiApplication.main(ApiApplication.java:10) ~(main/:na)

Caused by: java.lang.NullPointerException: Cannot invoke “org.springframework.web.servlet.mvc.condition.PatternsRequestCondition.toString()” because the return value of “springfox.documentation.spi.service.contexts.Order .RequestHandler)” is null

at springfox.documentation.spi.service.contexts.Orderings$8.compare(Orderings.java:112) ~(springfox-spi-2.9.2.jar:null)

at springfox.documentation.spi.service.contexts.Orderings$8.compare(Orderings.java:109) ~(springfox-spi-2.9.2.jar:null)

at com.google.common.collect.ComparatorOrdering.compare(ComparatorOrdering.java:37) ~(guava-20.0.jar:na)

at java.base/java.util.TimSort.countRunAndMakeAscending(TimSort.java:355) ~(na:na)

at java.base/java.util.TimSort.sort(TimSort.java:220) ~(na:na)

at java.base/java.util.Arrays.sort(Arrays.java:1233) ~(na:na)

at com.google.common.collect.Ordering.sortedCopy(Ordering.java:855) ~(guava-20.0.jar:na)

at springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider.requestHandlers(WebMvcRequestHandlerProvider.java:57) ~(springfox-spring-web-2.9.2.jar:null)

at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper$2.apply(DocumentationPluginsBootstrapper.java:138) ~(springfox-spring-web-2.9.2.jar:null)

at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper$2.apply(DocumentationPluginsBootstrapper.java:135) ~(springfox-spring-web-2.9.2.jar:null)

at com.google.common.collect.Iterators$7.transform(Iterators.java:750) ~(guava-20.0.jar:na)

at com.google.common.collect.TransformedIterator.next(TransformedIterator.java:47) ~(guava-20.0.jar:na)

at com.google.common.collect.TransformedIterator.next(TransformedIterator.java:47) ~(guava-20.0.jar:na)

at com.google.common.collect.MultitransformedIterator.hasNext(MultitransformedIterator.java:52) ~(guava-20.0.jar:na)

at com.google.common.collect.MultitransformedIterator.hasNext(MultitransformedIterator.java:50) ~(guava-20.0.jar:na)

at com.google.common.collect.ImmutableList.copyOf(ImmutableList.java:249) ~(guava-20.0.jar:na)

at com.google.common.collect.ImmutableList.copyOf(ImmutableList.java:209) ~(guava-20.0.jar:na)

at com.google.common.collect.FluentIterable.toList(FluentIterable.java:614) ~(guava-20.0.jar:na)

at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.defaultContextBuilder(DocumentationPluginsBootstrapper.java:111) ~(springfox-spring-web-2.9.2.jar:null)

at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.buildContext(DocumentationPluginsBootstrapper.java:96) ~(springfox-spring-web-2.9.2.jar:null)

at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.start(DocumentationPluginsBootstrapper.java:167) ~(springfox-spring-web-2.9.2.jar:null)

at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:178) ~(spring-context-5.3.25.jar:5.3.25)

… 14 공통 프레임

위의 오류 코드가 표시되었지만, (springfox.documentation.RequestHandler)” is null 이 부분을 복사해서 구글한 곳 extends WebMvcConfigurationSupport 추가하는 방법이 있으므로 즉시 추가해 보았습니다.

거짓말처럼 오류가 사라지고 서버가 켜졌습니다!
기꺼이

localhost:(사용자 포트 번호)/swagger-ui.html에 연결하므로 연결할 수 없습니다.

Whitelabel 오류 페이지

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sat Mar 11 02:58:27 KST 2023
There was an unexpected error (type=Not Found, status=404).오류가 발생했습니다.

콘솔 창에

No mapping for GET /swagger-ui.html 이렇게 표시되어 복사 후 구글을 해 보았습니다.

@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/");
	  }

위의 코드를 추가할 수 있는 방법을 살펴보고 곧 추가했습니다.


차잔!
드디어 성공했습니다 ㅎㅎ

정확한 원리는 잘 모르겠지만, 잇달아 공부해 나가면서 알아가야 한다.

나와 같은 오류가 발생한 사람에게 도움이되기를 바랍니다.