java
主页 > 软件编程 > java >

SpringBoot2.6.x与Swagger3兼容问题及解决方法

2025-03-21 | 佚名 | 点击:

报错:

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

解决:

? 如果项目中未引入 spring-boot-starter-actuator,则直接在 yml 文件中加入如下配置:

1

2

3

4

spring:

  mvc:

    pathmatch:

      matching-strategy: ant-path-matcher

? 反之再添加如下配置:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

@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));

}

原文链接:
相关文章
最新更新