You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

添加SpringBoot Actuator 2.6.6与Swagger 3.0.0启动时遇空指针异常求助

解决Spring Boot 2.6.6中Actuator与Swagger共存启动空指针问题

这个问题的核心是Spring Boot 2.6+的路径匹配策略调整,加上Actuator引入的WebMvc端点组件,和旧版Swagger的DocumentationPluginBootstrapper初始化逻辑冲突导致空指针。以下是几种可行的解决方案:

方案1:升级Swagger依赖适配Spring Boot 2.6+

如果你使用的是springfox-swagger2/springfox-swagger-ui,必须升级到3.0.0及以上版本,旧版本未适配Spring Boot 2.6的路径匹配规则。Gradle依赖配置替换为:

implementation 'io.springfox:springfox-boot-starter:3.0.0'

注意:springfox-boot-starter会自动整合Swagger核心、UI及Spring Boot适配依赖,无需再单独引入springfox-swagger2或springfox-swagger-ui。

方案2:手动强制Swagger使用Ant路径匹配器

若无法升级Swagger版本,可通过自定义配置类,修改Swagger依赖的请求映射处理器路径匹配规则:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.lang.reflect.Field;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket api() {
        // 保留你的Swagger原有配置(如API扫描路径、信息描述等)
        return new Docket(springfox.documentation.spi.DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.your.package"))
                .paths(PathSelectors.any())
                .build();
    }

    // 修复路径匹配冲突,强制使用AntPathMatcher
    @Bean
    public RequestMappingHandlerMapping requestMappingHandlerMapping() throws NoSuchFieldException, IllegalAccessException {
        RequestMappingHandlerMapping handlerMapping = new RequestMappingHandlerMapping();
        Field pathMatcherField = RequestMappingHandlerMapping.class.getDeclaredField("pathMatcher");
        pathMatcherField.setAccessible(true);
        pathMatcherField.set(handlerMapping, new AntPathMatcher());
        return handlerMapping;
    }
}

方案3:排除Actuator冲突的自动配置类

尝试排除Actuator的WebMvcEndpointAutoConfiguration,避免其路径匹配逻辑干扰Swagger初始化:
在Spring Boot启动类添加排除配置:

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcEndpointAutoConfiguration;

@SpringBootApplication(exclude = {WebMvcEndpointAutoConfiguration.class})
public class YourApplication {
    public static void main(String[] args) {
        SpringApplication.run(YourApplication.class, args);
    }
}

注意:此方案可能会改变Actuator端点的默认路径,需验证/actuator/health等端点是否可正常访问。

验证步骤

  1. 保留application.properties中的spring.mvc.pathmatch.matching-strategy=ant-path-matcher配置
  2. 应用上述任一方案后,执行Gradle构建命令./gradlew clean bootRun启动项目
  3. 访问Swagger文档地址(默认http://localhost:8080/swagger-ui/)确认正常加载
  4. 访问Actuator健康端点http://localhost:8080/actuator/health确认返回正常状态

内容的提问来源于stack exchange,提问作者Manoj Madushanka

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 05:46:06