Springboot引入knife

springboot3以上版本改如何引入knife4j
最新版 的knife4j 4.2.0版本

该回答引用GPT:
在Springboot的项目中引入Knife4j最新版的方法如下:

  1. 在pom.xml文件中添加Knife4j的依赖:
<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-spring-boot-starter</artifactId>
    <version>4.2.0</version>
</dependency>
  1. 在Springboot配置文件application.yml(or .properties)中添加相关的配置:
# 接口文档信息
knife4j:
  swagger:
    enabled: true # 是否开启swagger
    title: demo-API # API文档标题
    description: demo项目API文档 # API文档描述
    contact:
      name: demo # 联系人姓名
      email: demo@example.com # 联系人邮箱
    version: 1.0.0 # API文档版本号
    license:
      name: Apache License 2.0 # 许可证名称
      url: https://www.apache.org/licenses/LICENSE-2.0.html # 许可证URL

以上配置文件中,主要关注knife4j.swagger.enabled属性,开启swagger后,即可访问接口文档和测试接口。

  1. 在Controller类方法上使用Swagger注解描述接口信息:
@RestController
@Api(tags = "用户管理")
@RequestMapping("/api/user")
public class UserController {

    @ApiOperation(value = "获取用户列表")
    @GetMapping("")
    public List<User> userList() {
        // ...
    }

    @ApiOperation(value = "获取指定ID的用户信息")
    @GetMapping("/{id}")
    public User getUser(@ApiParam(value = "用户ID") @PathVariable Long id) {
        // ...
    }

    @ApiOperation(value = "创建一个用户")
    @PostMapping("")
    public User createUser(@ApiParam(value = "用户信息") @RequestBody User user) {
        // ...
    }

    @ApiOperation(value = "更新指定ID的用户信息")
    @PutMapping("/{id}")
    public User updateUser(@ApiParam(value = "用户ID") @PathVariable Long id,
                            @ApiParam(value = "更新信息") @RequestBody User user) {
        // ...
    }

    @ApiOperation(value = "删除指定ID的用户")
    @DeleteMapping("/{id}")
    public void deleteUser(@ApiParam(value = "用户ID") @PathVariable Long id) {
        // ...
    }
}

在方法上使用Swagger的注解即可对接口进行描述,@Api用于描述Controller类,@ApiOperation用于描述方法,@ApiParam用于描述方法参数。

通过以上步骤,即可在Springboot项目中快速引入并使用Knife4j最新版,便于接口文档的管理和测试。
如还有疑问,可留言帮助解决。

knife4j,是干啥的呢。