Spring Boot 整合 Swagger2
1.添加pom依赖
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.webjars/bootstrap -->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>5.1.3</version>
</dependency>
2.添加swagger的配置类
@Configuration
//@EnableWebMvc
@EnableSwagger2
public class Swagger2 {
// swagger2的配置文件,这里可以配置swagger2的一些基本的内容,比如扫描的包等等
@Bean // 创建一个bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
// 为当前包路径
.apis(RequestHandlerSelectors.basePackage("com.etc.ssm.controller")).paths(PathSelectors.any()).build();
}
// 构建 api文档的详细信息函数,注意这里的注解引用的是哪个
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
// 页面标题
.title("Spring 测试使用 Swagger2 构建RESTful API")
// 创建人
.contact(new Contact("小白", "http://chinasofti.com", ""))
// 版本号
.version("1.0")
// 描述
.description("API 描述").build();
}
}
3.因为: Springboot2.6以后将SpringMVC 默认路径匹配策略从AntPathMatcher 更改为PathPatternParser,导致出错
会出现这个错误:org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper';
在配置文件application.properties中加入以下配置
spring.mvc.pathmatch.matching-strategy=ANT_PATH_MATCHER
如果是application.yml就加入
spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher