报错信息:
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2024-05-16 10:35:51.280 ERROR 3948 --- [ restartedMain] o.s.boot.SpringApplication : Application run failedorg.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerExceptionat org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:182) ~[spring-context-5.3.31.jar:5.3.31]at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:54) ~[spring-context-5.3.31.jar:5.3.31]at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:357) ~[spring-context-5.3.31.jar:5.3.31]at java.base/java.lang.Iterable.forEach(Iterable.java:75) ~[na:na]at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:156) ~[spring-context-5.3.31.jar:5.3.31]at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:124) ~[spring-context-5.3.31.jar:5.3.31]at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:946) ~[spring-context-5.3.31.jar:5.3.31]at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:594) ~[spring-context-5.3.31.jar:5.3.31]at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147) ~[spring-boot-2.7.18.jar:2.7.18]at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:732) ~[spring-boot-2.7.18.jar:2.7.18]at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:409) ~[spring-boot-2.7.18.jar:2.7.18]at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) ~[spring-boot-2.7.18.jar:2.7.18]at org.springframework.boot.SpringApplication.run(SpringApplication.java:1300) ~[spring-boot-2.7.18.jar:2.7.18]at org.springframework.boot.SpringApplication.run(SpringApplication.java:1289) ~[spring-boot-2.7.18.jar:2.7.18]at com.pc.sz.FileEncryptionAndDecryption.main(FileEncryptionAndDecryption.java:12) ~[main/:na]at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:50) ~[spring-boot-devtools-2.7.18.jar:2.7.18]
Caused by: java.lang.NullPointerException: nullat springfox.documentation.spring.web.WebMvcPatternsRequestConditionWrapper.getPatterns(WebMvcPatternsRequestConditionWrapper.java:56) ~[springfox-spring-webmvc-3.0.0.jar:3.0.0]at springfox.documentation.RequestHandler.sortedPaths(RequestHandler.java:113) ~[springfox-core-3.0.0.jar:3.0.0]at springfox.documentation.spi.service.contexts.Orderings.lambda$byPatternsCondition$3(Orderings.java:89) ~[springfox-spi-3.0.0.jar:3.0.0]at java.base/java.util.Comparator.lambda$comparing$77a9974f$1(Comparator.java:469) ~[na: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:1515) ~[na:na]at java.base/java.util.ArrayList.sort(ArrayList.java:1750) ~[na:na]at java.base/java.util.stream.SortedOps$RefSortingSink.end(SortedOps.java:392) ~[na:na]at java.base/java.util.stream.Sink$ChainedReference.end(Sink.java:258) ~[na:na]at java.base/java.util.stream.Sink$ChainedReference.end(Sink.java:258) ~[na:na]at java.base/java.util.stream.Sink$ChainedReference.end(Sink.java:258) ~[na:na]at java.base/java.util.stream.Sink$ChainedReference.end(Sink.java:258) ~[na:na]at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:485) ~[na:na]at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474) ~[na:na]at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913) ~[na:na]at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:na]at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578) ~[na:na]at springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider.requestHandlers(WebMvcRequestHandlerProvider.java:81) ~[springfox-spring-webmvc-3.0.0.jar:3.0.0]at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195) ~[na:na]at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1655) ~[na:na]at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484) ~[na:na]at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474) ~[na:na]at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913) ~[na:na]at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:na]at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578) ~[na:na]at springfox.documentation.spring.web.plugins.AbstractDocumentationPluginsBootstrapper.withDefaults(AbstractDocumentationPluginsBootstrapper.java:107) ~[springfox-spring-web-3.0.0.jar:3.0.0]at springfox.documentation.spring.web.plugins.AbstractDocumentationPluginsBootstrapper.buildContext(AbstractDocumentationPluginsBootstrapper.java:91) ~[springfox-spring-web-3.0.0.jar:3.0.0]
Caused by: java.lang.NullPointerException: nullat springfox.documentation.spring.web.plugins.AbstractDocumentationPluginsBootstrapper.bootstrapDocumentationPlugins(AbstractDocumentationPluginsBootstrapper.java:82) ~[springfox-spring-web-3.0.0.jar:3.0.0]at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.start(DocumentationPluginsBootstrapper.java:100) ~[springfox-spring-web-3.0.0.jar:3.0.0]at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:179) ~[spring-context-5.3.31.jar:5.3.31]... 19 common frames omitted
通过在网络上面检索找到问题的定位:
发现该报错和升级spring-boot有关,2.5 版本以上的 spring-boot和 swagger 版本不兼容:
看起来最新的spring boot版本2.7与Swagger 2不兼容。有两种解决方法。
1.尝试将springboot版本降级到2.5.0以下(一个简单的解决方法)。
1.将swagger升级到OpenApi,这可能被称为swagger 3。这对注解没有什么变化。通过遵循互联网上提供的指南,您可以轻松地从Swagger 2迁移到OpenApi 3。
升级swagger的第二个解决方案是更好的,因为springfox的swagger 2越来越旧,最新版本于2020年7月发布。OpenApi是最新的API规范。
- 除此之外,可以在代码中加入一个自定义的 processer 解决该问题:
package com.pc.sz.process;import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
import springfox.documentation.spring.web.plugins.WebFluxRequestHandlerProvider;
import springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider;import java.lang.reflect.Field;
import java.util.List;
import java.util.stream.Collectors;@Configuration
public class SwaggerErrorHandlerProcessor implements BeanPostProcessor {@Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {if (bean instanceof WebMvcRequestHandlerProvider || bean instanceof WebFluxRequestHandlerProvider) {customizeSpringfoxHandlerMappings(getHandlerMappings(bean));}return bean;}private <T extends RequestMappingInfoHandlerMapping> void customizeSpringfoxHandlerMappings(List<T> mappings) {List<T> copy = mappings.stream().filter(mapping -> mapping.getPatternParser() == null).collect(Collectors.toList());mappings.clear();mappings.addAll(copy);}@SuppressWarnings("unchecked")private List<RequestMappingInfoHandlerMapping> getHandlerMappings(Object bean) {try {Field field = ReflectionUtils.findField(bean.getClass(), "handlerMappings");field.setAccessible(true);return (List<RequestMappingInfoHandlerMapping>) field.get(bean);} catch (IllegalArgumentException | IllegalAccessException e) {throw new IllegalStateException(e);}}}
这段代码是一个Spring Bean后置处理器(BeanPostProcessor),用于定制Swagger的错误处理器。在postProcessAfterInitialization方法中,首先判断传入的bean是否是WebMvcRequestHandlerProvider或WebFluxRequestHandlerProvider的实例,如果是,则调用getHandlerMappings方法获取其handlerMappings属性的值(即RequestMappingInfoHandlerMapping列表),然后调用customizeSpringfoxHandlerMappings方法进行定制化处理。该方 ** 过滤掉PatternParser为null的映射,并重新将处理后的映射列表赋值给原始的handlerMappings属性。
getHandlerMappings方法通过反射获取bean对象的handlerMappings属性的值,并强制转换为List类型返回。整体逻辑可以理解为对Swagger的不符合要求的Handler Mapping进行定制化处理,确保其满足Swagger的需求。
- 导致该报错使用的依赖参考:
buildscript {ext {springBootVersion = '2.7.18'}repositories {mavenCentral()maven {url 'https://mirrors.huaweicloud.com/repository/maven/'}}dependencies {classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")}
}subprojects {apply plugin: 'java'apply plugin: 'org.springframework.boot'apply plugin: 'io.spring.dependency-management'[compileJava, compileTestJava, javadoc]*.options*.encoding = 'UTF-8'configurations {developmentOnlyruntimeClasspath {extendsFrom developmentOnly}compileonly {extendsFrom annotationProcessor}}repositories {mavenCentral()maven {url 'https://mirrors.huaweicloud.com/repository/maven/'}maven{url'https://developer.huawei.com/repo/'}}dependencies {runtimeOnly "mysql:mysql-connector-java:8.0.29"implementation 'com.google.code.gson:gson:2.8.9'implementation "org.springframework.boot:spring-boot-starter-validation"implementation "org.springframework.boot:spring-boot-starter-mail"implementation "org.springframework.boot:spring-boot-starter-cache"implementation "org.springframework.boot:spring-boot-starter-aop"implementation "org.springframework.boot:spring-boot-starter-web"implementation "org.springframework.boot:spring-boot-starter-actuator"implementation "io.micrometer:micrometer-registry-prometheus"// 该依赖已经包含implementation "io.springfox:springfox-boot-starter:3.0.0"implementation "com.github.xiaoymin:knife4j-spring-boot-starter:2.0.4"implementation "com.bmuschko:gradle-docker-plugin:6.6.1"implementation "com.google.guava:guava:32.0.0-jre"implementation "net.bytebuddy:byte-buddy:1.10.13"implementation "org.javassist:javassist:3.27.0-GA"implementation 'javax.xml.ws:jaxws-api:2.3.1'implementation "com.baomidou:mybatis-plus-boot-starter:3.3.0"implementation 'commons-pool:commons-pool:1.5.4'implementation 'org.apache.commons:commons-pool2'implementation 'commons-pool:commons-pool:1.5.4'implementation 'org.apache.commons:commons-pool2'implementation 'io.opentracing.contrib:opentracing-spring-jaeger-web-starter:3.3.1'compileonly 'org.projectlombok:lombok:1.18.10'annotationProcessor 'org.projectlombok:lombok:1.18.10'developmentOnly "org.springframework.boot:spring-boot-devtools"annotationProcessor "org.springframework.boot:spring-boot-configuration-processor"testImplementation 'org.junit.jupiter:junit-jupiter:5.7.0'testCompileOnly 'org.projectlombok:lombok:1.18.10'testAnnotationProcessor 'org.projectlombok:lombok:1.18.10'testImplementation 'org.mockito:mockito-core:3.6.28'//升级Log4j版本//implementation "org.apache. Logging. Log4j: Log4j-api:2.15.0"//implementation "org.apache. logging.log4j:log4j-core:2.15.0"}test {useJUnitPlatform()}
}