@ApiOperation用法详情
时间: 2023-07-14 08:11:49 浏览: 158
@ApiOperation是Swagger注解之一,用于API接口文档的生成。它可以标注在Controller类或Controller方法上,用于描述API接口的作用、参数、返回值等信息。具体使用方法如下:
1. 在Controller类上标注@Api注解,用于描述Controller的作用。
2. 在Controller方法上标注@ApiOperation注解,用于描述方法的作用。
3. 使用@ApiParam注解标注Controller方法的参数,用于描述参数的作用、类型、是否必填等信息。
4. 使用@ApiResponses注解标注Controller方法的返回值,用于描述返回值的类型、状态码等信息。
例如:
```java
@Api(value = "用户管理接口", tags = {"用户管理接口"})
@RestController
@RequestMapping("/user")
public class UserController {
@ApiOperation(value = "获取用户信息接口", notes = "根据用户ID查询用户信息")
@ApiImplicitParam(name = "userId", value = "用户ID", dataType = "int", required = true)
@GetMapping("/{userId}")
public User getUserInfo(@PathVariable Integer userId) {
return userService.getUserInfo(userId);
}
@ApiOperation(value = "添加用户接口", notes = "添加用户信息")
@ApiImplicitParams({
@ApiImplicitParam(name = "user", value = "用户信息", dataType = "User", required = true),
@ApiImplicitParam(name = "token", value = "用户权限Token", dataType = "String", required = true)
})
@PostMapping("/")
public Result addUser(@RequestBody User user, @RequestParam String token) {
return userService.addUser(user, token);
}
@ApiOperation(value = "修改用户接口", notes = "根据用户ID修改用户信息")
@ApiImplicitParams({
@ApiImplicitParam(name = "userId", value = "用户ID", dataType = "int", required = true),
@ApiImplicitParam(name = "user", value = "用户信息", dataType = "User", required = true),
@ApiImplicitParam(name = "token", value = "用户权限Token", dataType = "String", required = true)
})
@PutMapping("/{userId}")
public Result updateUser(@PathVariable Integer userId, @RequestBody User user, @RequestParam String token) {
return userService.updateUser(userId, user, token);
}
@ApiOperation(value = "删除用户接口", notes = "根据用户ID删除用户信息")
@ApiImplicitParams({
@ApiImplicitParam(name = "userId", value = "用户ID", dataType = "int", required = true),
@ApiImplicitParam(name = "token", value = "用户权限Token", dataType = "String", required = true)
})
@DeleteMapping("/{userId}")
public Result deleteUser(@PathVariable Integer userId, @RequestParam String token) {
return userService.deleteUser(userId, token);
}
}
```
在上面的例子中,我们使用了@Api、@ApiOperation、@ApiImplicitParam、@ApiImplicitParams注解来描述Controller类和Controller方法的作用、参数、返回值等信息。这些注解可以帮助我们自动生成API接口文档,方便开发人员查阅和使用。
阅读全文