所需依赖
<!--swagger3的配置-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
所需配置
spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher
该配置主要解决spring-boot-starter
在SpringBoot2.6
以上版本兼容问题,SpringBoot2.6
版本修改了路径匹配规则导致springfox
会报错,因此需要调整mvc路径匹配策略
自定义JavaConfig
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
@Configuration
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
// 分组名称
.groupName("aa")
// api描述信息
.apiInfo(apiInfo())
// 扫描配置 select
.select()
.paths(PathSelectors.any())
// 设置只扫描@RestController
.apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("标题:swagger3初测试").termsOfServiceUrl("https://blog.csdn.net/qq_45464560?spm=1018.2226.3001.5343")
.description("API接口")
.contact(new Contact("作者:coderzpw","https://blog.csdn.net/qq_45464560?spm=1018.2226.3001.5343",""))
.version("3.0").build();
}
}
添加控制层web接口
接下来我简单写一个web接口
访问swagger的UI界面
接下来启动项目,然后访问 http://localhost:8080/swagger-ui/ 便会到达swagger的UI界面
添加knife4j接口显示文档
如嫌弃swagger
的界面太丑,我们可以选择knife4j
接口文档界面
需要添加如下依赖:
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>