SpringBoot继承Swagger
<!--swagger依赖-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!--swagger ui-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
编写配置文件
@Configuration
@EnableSwagger2
public class Swagger2Config {
@Bean
public Docket api(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("myAPI")
.select()
//扫描的路径包,设置basePackage将包下所有被@Api表示的类的方法
.apis(RequestHandlerSelectors.basePackage("com.example.controller"))
//只显示admin下面的路径
//.paths(Predicates.and(PathSelectors.regex("/admin/.*")))
//处理上所有路径
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("api文档")
.description("接口描述")
.version("1.0")
.contact(new Contact("xxx","http://xxx.com","xxx@xx.com"))
.build();
}
}
Swagger注解
Swagger使用的注解及其说明:
@Api:用在类上,说明该类的作用。
@ApiOperation:注解来给API增加方法说明。
@ApiParam:定义在参数上
@ApiResponses:用于表示一组响应
@ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息
- code:数字,例如400
- message:信息,例如"请求参数没填好"
- response:抛出异常的类
@ApiModel:描述一个Model的信息(一般用在请求参数无法使用@ApiImplicitParam注解进行描述的时候)
- @ApiModelProperty:描述一个model的属性
案例
@Data
@ApiModel(value="IntegralGrade对象", description="积分等级表")
public class IntegralGrade implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "编号")
private Long id;
@ApiModelProperty(value = "积分区间开始")
private Integer integralStart;
@ApiModelProperty(value = "积分区间结束")
private Integer integralEnd;
@ApiModelProperty(value = "借款额度")
private BigDecimal borrowAmount;
@ApiModelProperty(value = "创建时间")
private LocalDateTime createTime;
@ApiModelProperty(value = "更新时间")
private LocalDateTime updateTime;
@ApiModelProperty(value = "逻辑删除(1:已删除,0:未删除)")
private Boolean deleted;
}
@RestController
@RequestMapping("/admin/integralGrade")
@Api(value = "积分等级管理")
public class IntegralGradeController {
@Resource
private IntegralGradeService integralGradeService;
@GetMapping("/list")
@ApiOperation("积分等级列表")
public Result listAll(){
List<IntegralGrade> list = integralGradeService.list();
return Result.ok().data("list",list);
}
@DeleteMapping("/remove/{id}")
@ApiOperation(value = "根据id删除积分等级",notes = "逻辑删除")
public Result removeById(
@ApiParam(value = "数据id",required = true,example = "1")
@PathVariable Long id){
boolean result = integralGradeService.removeById(id);
if (result){
return Result.ok().message("删除成功");
}else {
return Result.error().message("删除失败");
}
}
@PostMapping("/save")
@ApiOperation(value = "新增积分等级")
public Result save(@ApiParam(value = "积分等级对象",required = true) @RequestBody IntegralGrade integralGrade){
boolean result = integralGradeService.save(integralGrade);
if (result){
return Result.ok().message("新增成功");
}else {
return Result.error().message("新增失败");
}
}
@PutMapping("/updateById")
@ApiOperation(value = "根据id修改积分等级")
public Result updateById(@ApiParam(value = "积分等级对象",required = true) @RequestBody IntegralGrade integralGrade){
boolean result = integralGradeService.updateById(integralGrade);
if (result){
return Result.ok().message("修改成功");
}else {
return Result.error().message("修改失败");
}
}
@GetMapping("/getById/{id}")
@ApiOperation(value = "根据id查询积分等级")
public Result getById(@ApiParam(value = "数据id",required = true,example = "1") @PathVariable Long id){
IntegralGrade result = integralGradeService.getById(id);
if (result == null){
return Result.error().message("查询失败");
}else {
return Result.ok().data("integralGrade",result);
}
}
}
访问
完成上述代码,启动t程序,访问:http://localhost:8080/swagger-ui.html
测试接口
有一个需要注意的地方:
Conntroller中定义的方法必须在@RequestMapping中指定RequestMethod类型,否则Swagger会默认为全类型皆可访问, API列表中会生成7条项目。