swagger2 -配置-兼容问题
1.报错
新版的 springboot 与 swagger2 集成会出现异常
Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
或
The following configuration properties have incompatible values: [spring.mvc.pathmatch.matching-strategy, spring.mvc.pathmatch.use-suffix-pattern]
2.解决
新增配置
spring:
mvc:
pathmatch:
#兼容boot2.6.7处理swagger2问题
matching-strategy: ANT_PATH_MATCHER
加完启动项目试一下,如果有的项目至此,还报下面这个错的话:
Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
继续执行如下解决方案:
//再新增代码 bean(一般在Swagger2Configurations配置类中加上这两方法就行)
/**
* 增加如下配置可解决Springboot 2.6.7升级与Swagger2 不兼容问题
**/
@Bean
public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(WebEndpointsSupplier webEndpointsSupplier, ServletEndpointsSupplier servletEndpointsSupplier, ControllerEndpointsSupplier controllerEndpointsSupplier, EndpointMediaTypes endpointMediaTypes, CorsEndpointProperties corsProperties, WebEndpointProperties webEndpointProperties, org.springframework.core.env.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, org.springframework.core.env.Environment environment, String basePath) {
return webEndpointProperties.getDiscovery().isEnabled() && (StringUtils.hasText(basePath) || ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT));
}