14.1 引言
在实际项目开发中,对于接口文档的书写始终是程序员的痛点,没有谁愿意花费大量时间用于维护接口文档,因此会选择使用swagger来自动生成接口文档。
14.2 引入依赖
<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>
<!--第三方的ui界面的美化 地址为 /doc.html-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.3</version>
</dependency>
14.3 开发配置类
@Configuration
@EnableSwagger2//开启swagger
public class SwaggerConfig {
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.pathMapping("/")
.select()
//扫描接口的包
.apis(RequestHandlerSelectors.basePackage("com.woniu.controller"))
.paths(PathSelectors.any())
.build()
.apiInfo(new ApiInfoBuilder()
//标题
.title("springboot整合swagger")
//描述
.description("springboot整合swagger,详细信息......")
//版本
.version("1.0")
//联系人信息
.contact(new Contact("老胡","http://www.baidu.com","93502690@qq.com"))
//项目主页
.license("项目主页")
.licenseUrl("http://www.baidu.com")
.build());
}
}
14.4 开发接口
@RestController
@Api(tags = "用户服务接口信息")//用于描述接口类的相关信息,作用于类上
@ApiSort(value = 1)
public class UserController {
//restful风格
@GetMapping("/user/save/{username}/{password}")
//@ApiOperation用于描述接口方法,作用于方法上
@ApiOperation(value = "保存用户",notes = "<span style='color:red;'>用来保存所有用户的接口</span>")
@ApiOperationSort(2)//接口顺序
//@ApiResponses用于描述响应状态信息
@ApiResponses({
@ApiResponse(code = 200,message = "保存成功"),
@ApiResponse(code=401,message = "没有权限")
})
//@ApiImplicitParams用于描述接口参数
@ApiImplicitParams({
//dataType:参数类型
//paramType:参数由哪里获取 path->从路径中获取,query->?传参,body->ajax请求
@ApiImplicitParam(name = "username",value = "用户名",dataType = "String",paramType = "path",example = "tom"),
@ApiImplicitParam(name="password",value = "密码",dataType = "String",paramType = "path",example = "111")
})
public Map<String,Object> save(@PathVariable("username") String username, @PathVariable("password") String password){
System.out.println(username + ":" + password);
HashMap<String, Object> map = new HashMap<>();
map.put("success",true);
map.put("message","查询所有成功");
return map;
}
//?传参
@GetMapping("/user/save")
@ApiOperation(value = "保存用户",notes = "<span style='color:red;'>用来保存所有用户的接口</span>")
@ApiResponses({
@ApiResponse(code = 200,message = "保存成功"),
@ApiResponse(code=401,message = "没有权限")
})
@ApiImplicitParams({
@ApiImplicitParam(name = "username",value = "用户名",dataType = "String",paramType = "query",example = "tom"),
@ApiImplicitParam(name="password",value = "密码",dataType = "String",paramType = "query",example = "111")
})
public Map<String,Object> save1(String username,String password){
System.out.println(username + ":" + password);
HashMap<String, Object> map = new HashMap<>();
map.put("success",true);
map.put("message","查询所有成功");
return map;
}
//ajax传参,无需设置@ApiImplicitParams
@PostMapping("/user/save")
@ApiOperation(value = "保存用户",notes = "<span style='color:red;'>用来保存所有用户的接口</span>")
@ApiResponses({
@ApiResponse(code = 200,message = "保存成功"),
@ApiResponse(code=401,message = "没有权限")
})
public Map<String,Object> save2(@RequestBody User user){
System.out.println(user.getUsername() + ":" + user.getPassword());
HashMap<String, Object> map = new HashMap<>();
map.put("success",true);
map.put("message","查询所有成功");
return map;
}
}
14.5 测试
# 启动项目后,访问localhost:8080/doc.html,可看到接口文档具体内容。