SpringBoot2.6.x集成Swagger报错,java.lang.NullPointerException,通过knife4j增强swagger,(附带swagger3.0x解决办法)

使用SpringBoot集成Swagger后,运行报错:Failed to start bean ‘documentationPluginsBootstrapper’; nested exception is java.lang.NullPointerException


第一种情况:

***swagger和knife4j均为2.x的版本:

 <!--Swagger-UI API文档生产工具-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
        </dependency>
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-boot-starter</artifactId>
            <version>2.0.4</version>
        </dependency>

这里是版本的问题,因为Springfox使用的路径匹配是基于AntPathMatcher的,而Spring Boot 2.6.X使用的是PathPatternMatcher。
解决方法有两种:

第一:降低springboot版本:
把springboot回退到2.5.6就能正常启动
第二:修改配置文件:增加:
spring.mvc.pathmatch.matching-strategy=ANT_PATH_MATCHER
在这里插入图片描述
访问http://localhost:8001/swagger-ui.html 成功集成:
在这里插入图片描述

通过knife4j增强Swagger,界面更美观,功能也强大:
增加依赖:

<dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-boot-starter</artifactId>
            <version>2.0.4</version>
        </dependency>

swagger配置类上面加注解:
@EnableKnife4j
在这里插入图片描述
访问http://localhost:8001/doc.html 成功集成:
在这里插入图片描述

参考链接:Failed to start bean ‘documentationPluginsBootstrapper‘; nested exception is java.lang.NullPointerEx

springboot集成swagger3出现如下错误:Failed to start bean ‘documentationPluginsBootstrapper‘; nested exception

后面可能会有没有controller的情况,这里我使用的一种方法,不用修改yml文件,而是在启动类上加注解@EnableWebMvc,swagger也能正常运行

第二种情况:

Swagger版本3.0.0,knife4j版本3.0.3

		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
<!--Swagger-UI knife4j增强-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-boot-starter</artifactId>
            <version>3.0.3</version>
        </dependency>

这里需要注意:高版本不需要@EnableWebMvc,否则会报错,yml文件一样需要配置:
在这里插入图片描述
swagger配置:这里需要去掉@EnableSwagger2

@Configuration
@EnableKnife4j
public class SwaggerConfig  {

    @Bean
    public Docket createRestApi(Environment env) {
        return new Docket(DocumentationType.OAS_30)
                .useDefaultResponseMessages(false)
                .groupName("1.0版本")
                .apiInfo(apiInfo())
                //只有当springboot配置文件为dev或test环境时,才开启swaggerAPI文档功能
//                .enable(flag)
                .enable(true)
                .select()
                // 这里指定Controller扫描包路径:设置要扫描的接口类,一般是Controller类
                .apis(RequestHandlerSelectors.any())  //这里采用包扫描的方式来确定要显示的接口
//                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) //这里采用包含注解的方式来确定要显示的接口
                // 配置过滤哪些,设置对应的路径才获取
//                .paths(PathSelectors.regex("/eid/.*?"))
                .paths(PathSelectors.any())
                .build();
        //防止Controller中参数中没有实体类或者返回值不是实体类导致Swagger Models页面扫描不到的情况
//                .additionalModels(typeResolver.resolve(EmailNotice.class))
//                .additionalModels(typeResolver.resolve(SmsNotice.class))
//                .additionalModels(typeResolver.resolve(WeChatNotice.class));
    }

    ///配置相关的api信息
    private ApiInfo apiInfo()
    {
        return new ApiInfoBuilder()
                .description("smartherb调试文档")
                //作者信息
                .contact(new Contact("smartherb", "http://127.0.0.1:9310/doc.html", "@163.com"))
                .version("v1.0")
                .title("smartherb服务端API文档")
                //服务Url
                .termsOfServiceUrl("")
                .build();
    }


    @Bean
    public WebMvcEndpointHandlerMapping webMvcEndpointHandlerMapping(WebEndpointsSupplier wes
            , ServletEndpointsSupplier ses, ControllerEndpointsSupplier ces, EndpointMediaTypes emt
            , CorsEndpointProperties cep, WebEndpointProperties wep, Environment env) {
        List<ExposableEndpoint<?>> allEndpoints = new ArrayList<>();
        Collection<ExposableWebEndpoint> webEndpoints = wes.getEndpoints();
        allEndpoints.addAll(webEndpoints);
        allEndpoints.addAll(ses.getEndpoints());
        allEndpoints.addAll(ces.getEndpoints());
        String basePath = wep.getBasePath();
        EndpointMapping endpointMapping = new EndpointMapping(basePath);
        boolean shouldRegisterLinksMapping = shouldRegisterLinksMapping(wep, env, basePath);
        return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints, emt
                , cep.toCorsConfiguration(), new EndpointLinksResolver(
                allEndpoints, basePath), shouldRegisterLinksMapping, null);
    }

    /**
     * shouldRegisterLinksMapping
     *
     * @param wep
     * @param env
     * @param basePath
     * @return
     */
    private boolean shouldRegisterLinksMapping(WebEndpointProperties wep, Environment env, String basePath) {
        return wep.getDiscovery().isEnabled() && (StringUtils.hasText(basePath) || ManagementPortType.get(env).equals(ManagementPortType.DIFFERENT));
    }



}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值