Cannot invoke “org.springframework.web.servlet.mvc.condition.PatternsRequestCondition.getPatterns()“

遇到问题:Cannot invoke "org.springframework.web.servlet.mvc.condition.PatternsRequestCondition.getPatterns()" because "this.condition" is null

分析原因:

spring boot升级到2.7.0,项目启动报错,项目使用swagger 3.0 ,具体版本是knife4j-spring-boot-starter的3.0.3,查找解决办法,都说修改配置文件(但是修改配置后仍然无效)或者新增配置类,过滤空情况,仍然还是解决了很长时间没有用。
配置文件更改无效

解决方法:新增一个配置类或可放在启动类下,注意配置类不能放到swagger的配置类下,否则测试无效。

新建配置类:代码如下
package com.by.cloud;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
import springfox.documentation.spring.web.plugins.WebFluxRequestHandlerProvider;
import springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider;

import java.lang.reflect.Field;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Configuration
public class PostProcessorConfig {

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

}

项目运行结果:如下图

http://127.0.0.1:1000/doc.html

  • 9
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答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 MVCRequestMapping 条件之一使用,还需要检查对应的 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”为空”错误时,您应该检查代码是否正确注入了所有必需的组件,并检查您的配置文件是否是正确的,并包含必要的属性和依赖项。通过这种方式可以确保您的应用程序正常运行并正确地处理请求。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值