SpringBoot整合Swagger
前言
前后端分离后,维护接口文档基本上是必不可少的工作。一个理想的状态是设计好后,接口文档发给前端和后端,大伙按照既定的规则各自开发,开发好了对接上了就可以上线了。当然这是一种非常理想的状态,实际开发中却很少遇到这样的情况,接口总是在不断的变化之中,有变化就要去维护,做过的小伙伴都知道这件事有多么头大!还好,有一些工具可以减轻我们的工作量,Swagger2就是其中之一。
一、引入依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
二、配置Swagger
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.pathMapping("/")
.select()
.apis(RequestHandlerSelectors.basePackage("com.nvn.controller"))
.paths(PathSelectors.any())
.build().apiInfo(new ApiInfoBuilder()
.title("SpringBoot整合Swagger")
.description("SpringBoot整合Swagger,详细信息......")
.version("9.0")
.contact(new Contact("hahah","blog.csdn.net","lyra@gmail.com"))
.license("The Apache License")
.licenseUrl("http://www.baidu.com")
.build());
}
三、接口注解
- @Api注解可以用来标记当前Controller的功能。
- @ApiOperation注解用来标记一个方法的作用。
- @ApiImplicitParam注解用来描述一个参数,可以配置参数的中文含义,也可以给参数设置默认值,这样在接口测试的时候可以避免手动输入。
- 如果有多个参数,则需要使用多个@ApiImplicitParam注解来描述,多个@ApiImplicitParam注解需要放在一个@ApiImplicitParams注解中。
- 需要注意的是,@ApiImplicitParam注解中虽然可以指定参数是必填的,但是却不能代替@RequestParam(required = true),前者的必填只是在Swagger2框架内必填,抛弃了Swagger2,这个限制就没用了,所以假如开发者需要指定一个参数必填,@RequestParam(required = true)注解还是不能省略。
- 如果参数是一个对象(例如上文的更新接口),对于参数的描述也可以放在实体类中。例如下面一段代码:
@ApiModel
public class User {
@ApiModelProperty(value = "用户id")
private Integer id;
@ApiModelProperty(value = "用户名")
private String username;
@ApiModelProperty(value = "用户地址")
private String address;
//getter/setter
}
@RestController
@Api(tags = "增删查改")
public class TbDeptController {
@Resource
private TbDeptService TbDeptService;
@ApiOperation("查询所有部门信息接口")
@RequestMapping("quertwo")
private List<TbDeptEntity> quertwo() {
List<TbDeptEntity> list = null;
try {
list = TbDeptService.quertwo();
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
@ApiOperation("根据部门id添加信息")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "部门id", required = true),
@ApiImplicitParam(name = "DateName", value = "部门名称", required = true)
})
@RequestMapping("increasetwo")
private Integer increasetwo() throws Exception {
TbDeptEntity TbDeptEntity = new TbDeptEntity();
TbDeptEntity.setId(4);
TbDeptEntity.setDateName("开发部");
Integer count = TbDeptService.increasetwo(TbDeptEntity);
return count;
}
@ApiOperation("根据部门id删除数据")
@ApiImplicitParam(name = "id", value = "部门id", required = true)
@RequestMapping("deletetwo")
private Integer deletetwo() throws Exception {
Integer count = TbDeptService.deletetwo(3);
return count;
}
@ApiOperation("根据部门id修改数据")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "部门id", required = true),
@ApiImplicitParam(name = "DateName", value = "部门名称", required = true)
})
@RequestMapping("revisetwo")
private Integer revisetwo() throws Exception {
TbDeptEntity TbDeptEntity = new TbDeptEntity();
TbDeptEntity.setDateName("咸鱼");
TbDeptEntity.setId(1);
Integer count = TbDeptService.revisetwo(TbDeptEntity);
return count;
}
}