没有多余废话,就是干
spring-boot 2.7.8
springfox-boot-starter 3.0.0
结构
POM.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.8</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo3</name>
<description>demo3</description>
<url/>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-boot-starter -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
application.xml
#spring:
# application:
# name: demo3
spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher
server:
port: 8081
# 配置springdoc-openapi,用于文档化和访问API
springdoc:
# 配置Swagger UI的访问路径和排序方式
swagger-ui:
path: /swagger-ui.html # Swagger UI的访问路径
tags-sorter: alpha # 按字母顺序排序标签
operations-sorter: alpha # 按字母顺序排序操作
# 配置API文档的访问路径
api-docs:
path: /v3/api-docs # API文档的访问路径
# 配置API分组,用于组织和管理API
group-configs:
- group: 'default' # API分组名称
paths-to-match: '/**' # 匹配所有路径
packages-to-scan: com.ykx.easyexceldemo02.controller # 扫描的包,用于自动发现API
SwaggerConfig.java
package com.example.demo3.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.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableOpenApi
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.demo3.web")) // 替换为您的Controller所在的包路径
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("后端接口文档") // 文档标题
.description("前后端交互接口") // 文档路径
.version("1.0.0") // 文档版本
.build();
}
}
Controller.java
package com.example.demo3.web;
import com.example.demo3.entity.User;
import io.swagger.annotations.*;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;
import javax.servlet.http.HttpServletRequest;
@Api(tags = "控制类", hidden = true)
@RestController
@RequestMapping("/web")
public class Controller {
@ApiOperation(value = "测试")
@GetMapping("/come")
public String hello(@ApiParam(value = "主键id") String id) {
return "hello";
}
@ApiOperation("测试1")
@GetMapping("/come1")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "主键id"),
@ApiImplicitParam(name = "name", value = "用户名")
})
public String hello1(String id, String name, @RequestBody @ApiParam(value = "用户对象") User user) {
return "hello";
}
@ApiOperation("测试2")
@GetMapping("/come2")
public String hello2(@ApiParam(value = "注解id") String id) {
return "hello";
}
@ApiIgnore("忽略测试")
@ApiOperation("测试1")
@GetMapping("/come10")
public String hello10(@ApiParam(value = "请求对象") HttpServletRequest request) {
return "hello";
}
}
User.java
package com.example.demo3.entity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@ApiModel(value = "UserModel", description = "用户模型")
@Data
public class User {
@ApiModelProperty(value = "主键id", required = true, example = "1", hidden = true, dataType = "int", position = 1
, allowableValues = "1,2,3")
private int id;
@ApiModelProperty(value = "用户名", required = true, example = "zhangsan", position = 2)
private String name;
@ApiModelProperty(value = "年龄", required = true, example = "18", position = 3)
private int age;
}
效果图展示: