Springboot 2.6整合Swagger出现的问题

总览

解决方法一:启动类添加注解@EnableWebMvc
解决方法二:application.yml中添加

mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

问题

1、背景:idea,maven聚合工程

2、添加依赖(偷懒,直接父项目pom.xml添加)

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

3、创建一个swagger配置类
创建包config,创建类SwaggerConfig.java

package org.demo.config;

import *

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    //swagger会帮助我们生成接口文档
    /*
    * 1,配置文档信息
    * 2,配置生成信息
    * 3,Docket封装接口文档信息
    *
    * */
    @Bean
    public Docket getDocket(){
        Docket docket = new Docket(DocumentationType.SWAGGER_2);
        ApiInfoBuilder apiInfoBuilder = new ApiInfoBuilder();
        apiInfoBuilder.title("xx项目后端接口说明")
                .description("此文档详细说明了xx项目的后端接口规范...")
                .version("v 2.1")
                .contact(new Contact("F_M","www.demo.org","ll@163.com"));
        ApiInfo apiInfo = apiInfoBuilder.build();
        docket.apiInfo(apiInfo).select()
                .apis(RequestHandlerSelectors.basePackage("org.demo.controller"))
                .paths(PathSelectors.any())//.regex("/user/")
                .build();
        return docket;


    }


}

注意:@Configuration
@EnableSwagger2

要添加这两个注解
4、启动主程序
报错

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-01-25 13:45:33.566 ERROR 14480 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
	......

解决方法

百度中。。。。。。

解决方法一:启动类添加注解@EnableWebMvc
该注解就是为了引入一个DelegatingWebMvcConfiguration 配置类,而DelegatingWebMvcConfiguration又继承于WebMvcConfigurationSupport。也就是说,如果我们使用@EnableWebMvc就相当于导入了WebMvcConfigurationSupport类,这个时候,Spring Boot的自动装配就不会发生了,我们能用的,只有WebMvcConfigurationSupport提供的若干个配置。其实不使用@EnableWebMvc注解也是可以实现配置Webmvc,只需要将配置类继承于WebMvcConfigurationSupport类即可。
当使用@EnableWebMvc时,加载的是WebMvcConfigurationSupport中的配置项。
当不使用@EnableWebMvc时,使用的是WebMvcAutoConfiguration引入的配置项

@SpringBootApplication
@MapperScan(basePackages = {"org.demo.dao"})
@EnableWebMvc
public class ApiApplication {

    public static void main(String[] args) {
        SpringApplication.run(ApiApplication.class, args);
    }

}

在这里插入图片描述
继续百度。。。。。。
地址栏换成 http://localhost:8080/swagger-ui/index.html解决问题,我没有对路径进行任何配置,所以直接这样子访问即可
在这里插入图片描述
解决方法二
application.yml中添加

mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

在这里插入图片描述
启动项目,解决

在这里插入图片描述
这是因为Springfox使用的路径匹配是基于AntPathMatcher的,而Spring Boot 2.6.X使用的是PathPatternMatcher。

  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值