14.1 引言

在实际项目开发中,对于接口文档的书写始终是程序员的痛点,没有谁愿意花费大量时间用于维护接口文档,因此会选择使用swagger来自动生成接口文档。

14.2 引入依赖
  1. <dependency>
  2. <groupId>io.springfox</groupId>
  3. <artifactId>springfox-swagger2</artifactId>
  4. <version>2.9.2</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>io.springfox</groupId>
  8. <artifactId>springfox-swagger-ui</artifactId>
  9. <version>2.9.2</version>
  10. </dependency>
  11. <!--第三方的ui界面的美化 地址为 /doc.html-->
  12. <dependency>
  13. <groupId>com.github.xiaoymin</groupId>
  14. <artifactId>swagger-bootstrap-ui</artifactId>
  15. <version>1.9.3</version>
  16. </dependency>

14.3 开发配置类
  1. @Configuration
  2. @EnableSwagger2//开启swagger
  3. public class SwaggerConfig {
  4. @Bean
  5. public Docket docket(){
  6. return new Docket(DocumentationType.SWAGGER_2)
  7. .pathMapping("/")
  8. .select()
  9. //扫描接口的包
  10. .apis(RequestHandlerSelectors.basePackage("com.woniu.controller"))
  11. .paths(PathSelectors.any())
  12. .build()
  13. .apiInfo(new ApiInfoBuilder()
  14. //标题
  15. .title("springboot整合swagger")
  16. //描述
  17. .description("springboot整合swagger,详细信息......")
  18. //版本
  19. .version("1.0")
  20. //联系人信息
  21. .contact(new Contact("老胡","http://www.baidu.com","93502690@qq.com"))
  22. //项目主页
  23. .license("项目主页")
  24. .licenseUrl("http://www.baidu.com")
  25. .build());
  26. }
  27. }

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,可看到接口文档具体内容。