Spring Boot 2.6以后版本优雅解决PatternsRequestCondition.getPatterns()“ because “this.condition“ is null问题

特别提醒:

建议使用springfox升级到springdoc,springdoc是支持OPENAPIV3进行实现了;很好用,毕竟springfox已经好多年都不维护了,强烈建议更新到springdoc,更新比较简单,官网:GitHub - springdoc/springdoc-openapi: Library for OpenAPI 3 with spring-boot

其实网上已经有了,通过

spring.mvc.pathmatch.matching-strategy=ant_path_matcher

就可以了,这个我之前就这么用的,最近springboot版本升级到2.7.4了,也顺带了解了下springboot3.0版本升级事项,目前pathmatch 这个配置虽然在3.0也保留了,但是spring官网建议还是让我们调整;

上面问题产生的原因是swagger造成的,我相信大部分都因为这个;如果想省事就用上面的,但是我为了后面好升级,就找了下解决方案,通过这个问题,我发现,还是到官网去找,性能高一些

这是我通过官网介绍springboot3相关特性:

Preparing for Spring Boot 3.0



 

 在Issues · spring-projects/spring-boot · GitHub 操作解决方案

 Spring 5.3/Spring Boot 2.4 support · Issue #3462 · springfox/springfox · GitHub

这里有两种解决方案,第一个是我上面写的,第二个是webmvc处理器来进行处理的,我看了下评论有些局限性,大家可以尝试下 

public WebMvcRequestHandlerProvider(Optional<ServletContext> servletContext, HandlerMethodResolver methodResolver,
			List<RequestMappingInfoHandlerMapping> handlerMappings) {
		this.handlerMappings = handlerMappings.stream().filter(mapping -> mapping.getPatternParser() == null)
				.collect(Collectors.toList());

下面还有一种,也就是我现在使用的:

@Bean
public static BeanPostProcessor springfoxHandlerProviderBeanPostProcessor() {
    return new BeanPostProcessor() {

        @Override
        public 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);
            }
        }
    };
}

建议使用最后一个,其实解决思路和第二周大同小异;但是解决方案是基于

springfox 3.x Springfox Reference Documentation 而来的

  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 无法调用"org.springframework.web.servlet.mvc.condition.PatternsRequestCondition.getPatterns()",因为"this.condition"为空。 ### 回答2: 这个错误是由于在代码中尝试调用 `org.springframework.web.servlet.mvc.condition.PatternsRequestCondition` 类的 `getPatterns()` 方法时,该对象中的 `condition` 属性为 `null`,从而导致无法调用该方法而抛出的异常。 要解决这个问题,需要检查代码中用到 `PatternsRequestCondition` 对象的地方,并确认在使用之前已经按照要求对该对象进行了初始化。所以可以尝试以下解决方法: 1. 检查对象初始化:在使用 `PatternsRequestCondition` 对象之前,需要确保已经对其进行了初始化。此外,如果该对象作为 Spring MVC 的 RequestMapping 条件之一使用,还需要检查对应的 RequestMapping 是否已经正确配置。 2. 检查配置文件:如果 `PatternsRequestCondition` 对象的相关配置信息以某种方式存储在配置文件中,那么需要对该配置文件进行检查,确保其中没有对该对象的配置信息遗漏或错误。 3. 检查依赖包版本:如果使用的 Spring MVC 版本过低或者与依赖库冲突,也可能会导致该问题的发生。可以通过检查依赖库版本,或者升级到最新版本解决问题。 总之,该错误的原因可能有很多种,需要根据实际情况进行排查。以上是一些可能的解决方法,但无法保证一定能解决问题。 ### 回答3: 这个错误是因为在调用一个方法的时候,该方法的对象为空,因此无法执行该方法。在这种情况下,该错误提示是“不能调用“org.springframework.web.servlet.mvc.condition.patternsrequestcondition.getpatterns()”因为“this.condition”为空。 这个错误通常发生在Spring程序中,它是一个Web框架,该框架使用了多种组件,例如Controller,Service和Repository。当使用Spring框架构建Web应用程序时,需要将所有相关的对象和依赖项注入到Spring容器中,以确保它们能够正确运行。如果在Spring应用程序中没有正确注入组件,就会导致该错误的出现。 解决这个错误的方法是检查代码是否正确地注入了所有必需的组件。通常,这个错误是由于没有正确配置注入的组件所导致的。您应该确保您的代码中定义了必需的注解和依赖项,例如@Autowired和@Qualifier。此外,您还应该检查您的配置文件,以确保它们是正确的,并包含必要的属性和依赖项。 总结来说,当出现“不能调用“org.springframework.web.servlet.mvc.condition.patternsrequestcondition.getpatterns()”因为“this.condition”为空”错误时,您应该检查代码是否正确注入了所有必需的组件,并检查您的配置文件是否是正确的,并包含必要的属性和依赖项。通过这种方式可以确保您的应用程序正常运行并正确地处理请求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值