在Swagger 1.5.20中为API控制器添加描述,可以通过以下步骤实现:
@Api
,用于描述该API控制器的信息。例如:@Api(value = "UserController", description = "Operations pertaining to user management")
@RestController
@RequestMapping("/users")
public class UserController {
// ...
}
@ApiOperation
,用于描述该API方法的信息。例如:@ApiOperation(value = "Get a user by ID", response = User.class)
@GetMapping("/{id}")
public ResponseEntity<User> getUserById(@PathVariable Long id) {
// ...
}
@ApiParam
。例如:@ApiOperation(value = "Create a new user")
@PostMapping("/")
public ResponseEntity<User> createUser(@ApiParam(value = "User object", required = true) @RequestBody User user) {
// ...
}
@ApiResponses
和@ApiResponse
。例如:@ApiOperation(value = "Get all users", response = User.class, responseContainer = "List")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Successfully retrieved users"),
@ApiResponse(code = 401, message = "You are not authorized to view the resource"),
@ApiResponse(code = 403, message = "Accessing the resource you were trying to reach is forbidden"),
@ApiResponse(code = 404, message = "The resource you were trying to reach is not found")
})
@GetMapping("/")
public ResponseEntity<List<User>> getAllUsers() {
// ...
}
通过以上步骤,你可以在Swagger 1.5.20中为API控制器添加描述。请注意,Swagger 1.5.20是较旧的版本,建议升级到最新版本以获得更多功能和改进。对于更详细的Swagger注解和配置信息,可以参考Swagger官方文档:Swagger Annotations。
领取专属 10元无门槛券
手把手带您无忧上云