Swagger
快速生成文档
导入配置
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
配置Application
@SpringBootApplication
@EnableSwagger2
public class DemoApplication {
public static void main(String[] args){
SpringApplication.run(DemoApplication.class);
}
}
增加注释
@ApiModelProperty(value = "用户年龄起始值")
private int age;
@ApiOperation(value = "获取用户详情")
public User getInfo(@ApiParam(value = "用户ID") @PathVariable String id){
...
}
常用注释
- @ApiIgnore:使用注解忽略该API,不会参与文档生成
- @ApiOperation:描述该api,如: @ApiOperation(value=”创建用户”, notes=”根据User对象创建用户”)
- 请求方法:@RequestMapping(value = “user”, method = RequestMethod.POST)
- 参数x信息:@ApiImplicitParam(name = “user”, value = “用户详细实体user”, required = true, dataType = “User”)
- @Api:修饰整个类,描述Controller的作用
- @ApiParam:单个参数描述
- @ApiModel:用对象来接收参数
- @ApiResponses:HTTP响应整体描述
- @ApiProperty:用对象接收参数时,描述对象的一个字段
网友评论