完整异常信息:
Cannot invoke “org.springframework.web.servlet.mvc.condition.PatternsRequestCondition.getPatterns()” because “this.condition” is null
异常场景:
springboot2.7.3 搭配 springfox 3.0.0
出现原因:
springfox 3.0.0 错误导致
解决办法:
配置文件中加入
spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher
swagger 配置类:
/**
* Swagger配置文件。 [Springfox官方集成文档](http://springfox.github.io/springfox/docs/current/)
* [Swagger注解官方文档](https://github.com/swagger-api/swagger-core/wiki/Annotations-1.5.X)
*
* <p>配置注意事项: 1. swagger及swagger-ui的添加,注意版本 2. 该config文件的注解添加,@Configuration与@EnableSwagger2为必须添加 3.
* 配置servlet-mapping为"/",因为会生成静态文件,因此需要注意路径穿透
*/
@EnableOpenApi
@Configuration
public class SwaggerConfig {
/**
* 配置基本信息
*
* @return
*/
@Bean
public ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Swagger Test App Restful API")
.description("swagger test app restful api")
.termsOfServiceUrl("https://github.com/xxx")
.contact(
new Contact("我是顾子晨", "https://blog.youkuaiyun.com/gu_zhang_w?type=blog", "my@hotmail.com"))
.version("1.0")
.build();
}
/**
* 配置文档生成最佳实践
*
* @param apiInfo
* @return
*/
@Bean
public Docket createRestApi(ApiInfo apiInfo) {
return new Docket(DocumentationType.OAS_30)
.apiInfo(apiInfo)
.groupName("SwaggerGroupOneAPI")
.select()
.apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
.paths(PathSelectors.any())
.build();
}
/** 增加如下配置可解决Spring Boot 6.x 与Swagger 3.0.0 不兼容问题 */
@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));
}
}
其他:
如果你访问github网站比较慢,或者一些资源无法访问你可以看一下这里:网络加速器
关于我
- 关注不迷路,点赞走一波~ 转载请标注~
- 公众号