引入依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<dependency>  
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>

<!--ui插件-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-openapi2-spring-boot-starter</artifactId>
<version>4.0.0</version>
</dependency>

添加配置

在项目下建立 config/Swaggerconfig 文件

image.png
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.example.neo4j_test.config;  

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
public class SwaggerConfig {

@Bean
public Docket docket() {
ApiInfoBuilder apiInfoBuilder = new ApiInfoBuilder();
apiInfoBuilder
.title("neo4j接口测试")
.description("swagger测试接口")
.version("v1.0.0");
ApiInfo apiInfo = apiInfoBuilder.build();
return new Docket(DocumentationType.OAS_30)
.apiInfo(apiInfo)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}

配置 application. properties

1
spring.mvc.pathmatch.matching-strategy=ant_path_matcher

运行 springboot 项目

点击进入: http://localhost:8080/doc.html

api ui 优化

swagger 提供了⼀套注解,可以对每个接⼝进⾏详细说明

@Api 类注解,在控制器类添加此注解,可以对控制器类进⾏功能说明

1
@Api(value="提供知识图谱中用户的基本操作",tags = "用户管理")

参考文档: Springboot2.7整合Swagger3 - 菜月昴 - 博客园 swagger-bootstrap-ui 介绍和使用_hundan_520520的博客-CSDN博客 快速开始 | Knife4j