高版本spring整合swagger报空指针错误:Failed to start bean ‘documentationPluginsBootstrapper‘;No operations

高版本的spring会和swagger有版本冲突,会导致系统报错误
Failed to start bean ‘documentationPluginsBootstrapper’; nested exception is java.lang.NullPointerException

swagger依赖

我们可以检查swagger依赖

<dependency>
	<groupId>io.springfox</groupId>
	<artifactId>springfox-boot-starter</artifactId>
	<version>3.0.0</version>
</dependency>

swaggerConfig文件

在swagger中添加BeanPostProcessor,此时项目可以正常启动

@Configuration
@EnableSwagger2
public class Swagger2Config {
    /**
     * 添加摘要信息(Docket)
     */
    @Bean
    public Docket controllerApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        .title("标题:接口")
                        .description("描述:接口文档。")
                        .version("版本号:1.0.0001")
                        .build())
                .useDefaultResponseMessages(false)
                .genericModelSubstitutes(DeferredResult.class)
                .forCodeGeneration(false)
                .select()
                .paths(PathSelectors.any())
                .build();
    }
     @Bean
    public static BeanPostProcessor springfoxHandlerProviderBeanPostProcessor() {
        return new BeanPostProcessor() {

            @Override
            public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
                if (bean instanceof WebMvcRequestHandlerProvider) {
                    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);
                }
            }
        };
    }
}

访问swagger地址提示No operations defined in spec!

这个方法可以在配置文件中增加配置项

spring.mvc.pathmatch.matching-strategy=ANT_PATH_MATCHER

此时可以正常访问swagger文档和项目。

高版本spring整合swagger报空指针错误:Failed to start bean ‘documentationPluginsBootstrapper‘;No operations_第1张图片

你可能感兴趣的:(问题排查,spring,java,后端,swagger,swagger报错,spring,cloud,spring,boot)