knife4j官方文档:https://doc.xiaominfo.com/docs/quick-start/start-knife4j-version
增强模式文档:https://doc.xiaominfo.com/docs/features/enhance
访问Knife4j的文档地址:http://ip:port/doc.html
1.用法
(1)控制层 @Tag(name = “用户信息管理”)
@Tag(name = "用户信息管理")
@RestController
@RequestMapping("/test/user")
public class TestController {
}
(2)方法 @Operation(summary = “获取用户列表”),@Parameter(name = “userId”)
@Operation(summary = "获取用户列表")
@GetMapping("/list")
public R<List<UserEntity>> userList() {
List<UserEntity> userList = new ArrayList<UserEntity>(users.values());
return R.ok(userList);
}
@Operation(summary = "获取用户详细")
@Parameter(name = "userId", description = "用户ID", required = true)
@GetMapping("/{userId}")
public R<UserEntity> getUser(@PathVariable Integer userId) {
if (!users.isEmpty() && users.containsKey(userId)) {
return R.ok(users.get(userId));
} else {
return R.fail("用户不存在");
}
}
(3)实体
@Tag(name = "UserEntity", description = "用户实体")
class UserEntity {
@Schema(title = "用户id")
private Integer userId;
}
2.@Schema注解属性说明
name:属性字段,注:该属性可以不写
title:标题
description:标题的描述
title和description一起使用
@Schema(name = "userId",title = "用户id",description = "长整型")
private Integer userId;
3.token设置
4.使用实体类来接收get请求对象,对象显示在同一个参数里面的问题
方式一:
解决办法: 方法里面增加 @ParameterObject 注解
@Operation(summary = "列表")
@GetMapping("/list")
public TableDataInfo<List<Product>> list(@ParameterObject Product product) {
startPage();
List<Product> list = productService.getList(product);
return getDataTable(list);
}
方式二: 推荐使用
https://doc.xiaominfo.com/docs/faq/v4/knife4j-parameterobject-flat-param
5.PostMapping PutMapping 接收参数,跟之前一样,加注解@RequestBody
@RepeatSubmit
@PostMapping("/add")
@ApiOperation("新增收获地址")
public AjaxResult add(@RequestBody TAddress tAddress) {
return tAddressService.addAddress(tAddress);
}
@RepeatSubmit
@PutMapping("/edit")
@ApiOperation("修改收获地址")
public AjaxResult edit(@RequestBody TAddress tAddress) {
return tAddressService.editAddress(tAddress);
}