Failed to start bean ‘documentationPluginsBootstrapper‘; nested exception is java.lang.NullPointerEx

该问题已解决!特此记录!欢迎指点分享!

目录

一 · 问题发生背景:

二 · 框架版本:

三 · 解决方案(着急可略过1 直接看 2 )

1、网上大概有 几种:

2、我的方案 ,摒弃杂念从根本入手

(1)继承  mvc的配置类  WebMvcConfigurationSupport 

(2)实现 WebMvcConfigurer  接口

四 · 完整代码


一 · 问题发生背景:

公司 Springboot1.5 - 升级 Springboot 2.7

对拦截器不太明白时候,可以参考以下博文:

Springboot 拦截器(Interceptor)详解_、楽.的博客-CSDN博客_interceptor springboot

Spring Boot拦截器(Interceptor)详解_时间漏斗的博客-CSDN博客_interceptor order

二 · 框架版本:

Springboot 2.7

Swagger 2.9.0

· 问题复现

项目 启动报错(如图):
org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException

三 · 解决方案(着急可略过1 直接看 2 )

1、网上大概有 几种:

(1)swagger版本问题,升级3.0版本 (亲测对我项目无用)

(2)拦截器自定义各种写法,各种路径放开 (个人感觉 越看越迷)

(3)properties文件 的更改 (亲测对我项目无用)

2、我的方案 ,摒弃杂念从根本入手

Springboot1.5 继承 WebMvcConfigurerAdapter的写法,Springboot2.7版本升级已经不支持了,如图:

经查找资料理清有两种方式可实现自定义拦截器:

(1)继承  mvc的配置类  WebMvcConfigurationSupport 

a、springboot2.0以后,mvc配置类从 WebMvcConfigurerAdapter 变成了  WebMvcConfigurationSupport,因此写法如下:

b、 需格外注意的是,在springboot2.0.0之后继承WebMvcConfigurationSupport类,要重写addInterceptorsaddResourceHandlers 方法,以及 配置好静态资源路径  ,尤其是swagger-ui.html,不然会导致404,当然也不要忘记根据需要写跨域方法:

 c、跨域 的  allowedOriguns("*") 也 变成了 allowedOriginPatterns("*"):

 至此,项目正常启动,一切正常了,第一种方法结束;

(2)实现 WebMvcConfigurer  接口

a、实现 WebMvcConfigurer接口,一定要加上@EnableWebMvc 注解,告诉spring 我要完全自己控制mvc配置,也就是说所有配置自己重写,所有默认配置都没了;有博主说到这样写,有时会导致很多请求进不来,或者参数转换出错之类的,因为spring mvc默认的转换器已经不生效了,包括全局配置的Jackson也会失效;也不排除这种可能。

(该博主的博文参考:https://blog.csdn.net/weixin_43831204/article/details/110322060?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-1-110322060-blog-94445071.pc_relevant_downloadblacklistv1&spm=1001.2101.3001.4242.2&utm_relevant_index=4

当然,目前我没有遇到,因为我重写的方法路径该放开的放开,该拦截的拦截了。

如果此处不加@EnableWebMvc 注解会报 文章开始的错误,导致项目运行不起来!!

b、加好@EnableWebMvc注解后,也不再需要 重写的方法了,因为会报红报错,所以注释或删掉 super.addInterceptors(registry); 、super.addResourceHandlers(registry);  并且同样要写好放开静态资源路径的代码,如图:

 至此,方法二启动成功,项目一切正常了。

四 · 完整代码

继承  mvc的配置类  WebMvcConfigurationSupport 的完整代码:

package cn.cinfotech.common.config;


import cn.cinfotech.filter.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

import javax.annotation.Resource;


/**
 * @author fantong
 * Springmvc 拦截器注册
 */
@Configuration
//@EnableWebMvc
//public class InterceptorConfig extends WebMvcConfigurerAdapter {
public class InterceptorConfig extends WebMvcConfigurationSupport {

//public class InterceptorConfig implements WebMvcConfigurer {
//    @Autowired
    @Resource
    private LoginInterceptor loginInterceptor;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        //注册拦截器

//        registry.addInterceptor(new AuthorizationInterceptor()).excludePathPatterns("/**");
        registry.addInterceptor(loginInterceptor)                       
                .addPathPatterns("/**")                                
                .excludePathPatterns("/user/XXXXXX/XXXXXX","/XXXXXX/XXXXXX","/user/XXXXXX/XXXXXX","/XXXXXX/v2/alipay_callback", "/XXXXXX/iosPay","/error","/XXXXXX/v2/wxCallBack","/XXXXXX/v2/XXXXXX","/swagger-resources/**","/webjars/**","/static/**","/swagger-ui.html/**");//"/qrcode/v2/pc/inquireLoginState",
//                .excludePathPatterns(String.valueOf(excludePath));
        //在springboot2.0.0之后继承WebMvcConfigurationSupport类,重写addInterceptors方法
        //     *
       super.addInterceptors(registry);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/")
                .addResourceLocations("classpath:/resources/");
        registry.addResourceHandler("/templates/**").addResourceLocations("classpath:/templates/");

        registry.addResourceHandler("swagger-ui.html","doc.html").addResourceLocations("classpath:/META-INF/resources/");
        //Windows下
        registry.addResourceHandler("/uploads2/**").addResourceLocations("file:D:/springbootFile/upload/images/");
        //Mac或Linux下(没有CDEF盘符)
        registry.addResourceHandler("/uploads/**").addResourceLocations("file:/home/qmake/workspace/images/userImg/");
        super.addResourceHandlers(registry);


    }


    /**
     * 跨域支持
     * @author QianChi
     * @date 2021/6/30 9:55
     *
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
//                .allowedOrigins("*")
                .allowedOriginPatterns("*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "DELETE", "PUT", "PATCH", "OPTIONS", "HEAD")
                .maxAge(3600 * 24);
    }
}

 实现WebMvcConfigurer 接口的完成拦截器 代码:

package cn.cinfotech.common.config;


import cn.cinfotech.filter.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

import javax.annotation.Resource;


/**
 * @author fantong
 * Springmvc 拦截器注册
 */
@Configuration
@EnableWebMvc
//public class InterceptorConfig extends WebMvcConfigurerAdapter {
//public class InterceptorConfig extends WebMvcConfigurationSupport {

public class InterceptorConfig implements WebMvcConfigurer {
//    @Autowired
    @Resource
    private LoginInterceptor loginInterceptor;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        //注册拦截器

//        registry.addInterceptor(new AuthorizationInterceptor()).excludePathPatterns("/**");
        registry.addInterceptor(loginInterceptor)                       
                .addPathPatterns("/**")                                
                .excludePathPatterns("/user/XXXXXX/XXXXXX","/XXXXXX/XXXXXX","/user/XXXXXX/XXXXXX","/XXXXXX/v2/alipay_callback", "/XXXXXX/iosPay","/error","/XXXXXX/v2/wxCallBack","/XXXXXX/v2/XXXXXX","/swagger-resources/**","/webjars/**","/static/**","/swagger-ui.html/**");//"/qrcode/v2/pc/inquireLoginState",
//                .excludePathPatterns(String.valueOf(excludePath));
        //在springboot2.0.0之后继承WebMvcConfigurationSupport类,重写addInterceptors方法
        //     *
//        super.addInterceptors(registry);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/")
                .addResourceLocations("classpath:/resources/");
        registry.addResourceHandler("/templates/**").addResourceLocations("classpath:/templates/");

        registry.addResourceHandler("swagger-ui.html","doc.html").addResourceLocations("classpath:/META-INF/resources/");
        //Windows下
        registry.addResourceHandler("/uploads2/**").addResourceLocations("file:D:/springbootFile/upload/images/");
        //Mac或Linux下(没有CDEF盘符)
        registry.addResourceHandler("/uploads/**").addResourceLocations("file:/home/qmake/workspace/images/userImg/");
//        super.addResourceHandlers(registry);


    }


    /**
     * 跨域支持
     * @author QianChi
     * @date 2021/6/30 9:55
     *
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
//                .allowedOrigins("*")
                .allowedOriginPatterns("*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "DELETE", "PUT", "PATCH", "OPTIONS", "HEAD")
                .maxAge(3600 * 24);
    }
}

特此记录,踩坑日志

转载请注明出处:

https://blog.csdn.net/LQCV587/article/details/125406749?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22125406749%22%2C%22source%22%3A%22LQCV587%22%7D&ctrtid=u3boJ

  • 13
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值