[springboot 开发单体web shop] 4. Swagger生成Javadoc
目录
可以看到,我们在yml
文件中配置的信息,展示在了页面的顶部,点击用户管理
:
从上图可以看出,我们的/users/create
接口展出出来,并且要传入的参数,请求类型等等信息都已经展示在上图中。
但是,要传递的参数是什么意思,都是我们的字段信息,我们要如何让它更友好的展示给调用方呢?让我们继续
完善我们的文档信息:完善说明信息
在我们创建用户的时候,需要传递一个
com.liferunner.dto.UserRequestDTO
对象,这个对象的属性如下:@RestController @RequestMapping(value = "/users") @Slf4j @Api(tags = "用户管理") public class UserController { @Autowired private IUserService userService; @ApiOperation(value = "用户详情", notes = "查询用户") @ApiIgnore @GetMapping("/get/{id}") //@GetMapping("/{id}") 如果这里设置位这样,每次请求swagger都会进到这里,是一个bug public String getUser(@PathVariable Integer id) { return "hello, life."; } @ApiOperation(value = "创建用户", notes = "用户注册接口") @PostMapping("/create") public JsonResponse createUser(@RequestBody UserRequestDTO userRequestDTO) { //... } }
@Data @AllArgsConstructor @NoArgsConstructor @Builder @ApiModel(value = "创建用户DTO", description = "用户注册需要的参数对象") public class UserRequestDTO { @ApiModelProperty(value = "用户名", notes = "username", example = "isaaczhang", required = true) private String username; @ApiModelProperty(value = "注册密码", notes = "password", example = "12345678", required = true) private String password; @ApiModelProperty(value = "确认密码", notes = "confimPassword", example = "12345678", required = true) private String confirmPassword; }
可以看到,我们有很多通过
@Apixxx
开头的注解说明,这个就是Swagger提供给我们用以说明字段和文档说明的注解。@Api
表示对外提供API@ApiIgnore
表示不对外展示,可用于类和方法@ApiOperation
就是指的某一个API下面的CURD动作@ApiResponses
描述操作可能出现的异常情况