总览
解决方法一:启动类添加注解@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。