【转载请注明】:
原文出处: 作者:jstarseven 码字挺辛苦的.....
一、Swagger介绍
Swagger是一个规范和完整的框架,用于生成、描述、调用和可视化RESTful风格的web服务。目标是使客户端和文件系统作为服务器以同样的速度来更新文件的方法,参数和模型紧密集成到服务器。这个解释简单点来讲就是说,swagger是一款可以根据restful风格生成的接口开发文档,并且支持做测试的一款中间软件。
二、使用swagger优势
1、对于后端开发人员来说
- 不用再手写Wiki接口拼大量参数,避免手写错误
- 对代码侵入性低,采用全注解的方式,开发简单
- 方法参数名修改、新增、减少参数都可以直接生效,不用手动维护
- 缺点:增加了开发成本,写接口还得再写一套参数配置
2、对前端开发来说
- 后端只需要定义好接口,会自动生成文档,接口功能、参数一目了然
- 联调方便,如果出了问题,直接测试接口,实时检查参数和返回值,就可以快速定位是前端还是后端的问题
3、对于测试来说
- 但对于测试没有前端界面UI的功能,可以直接用它来测试接口
- 操作简单,不用了解具体代码就可以操作
三、springboot集成swagger使用
1、新建maven项目(结构如下:)
2、配置pom.xml文件
1 24 4.0.0 56 11org.springframework.boot 7spring-boot-starter-parent 82.1.3.RELEASE 910 com.dds.sbswagger 12sb-swagger 130.0.1-SNAPSHOT 14sb-swagger 15Demo project for Spring Boot 16 1718 201.8 1921 4622 25org.springframework.boot 23spring-boot-starter-web 2426 30org.springframework.boot 27spring-boot-starter-test 28test 2931 35io.springfox 32springfox-swagger2 332.9.2 3436 40io.springfox 37springfox-swagger-ui 382.9.2 3941 45org.projectlombok 42lombok 431.18.6 4447 5448 5349 52org.springframework.boot 50spring-boot-maven-plugin 51
3、程序启动类
1 package com.dds.sbswagger; 2 3 import lombok.extern.slf4j.Slf4j; 4 import org.springframework.boot.SpringApplication; 5 import org.springframework.boot.autoconfigure.SpringBootApplication; 6 7 /** 8 * @author dds 9 */10 @SpringBootApplication11 @Slf4j12 public class SbSwaggerApplication {13 14 public static void main(String[] args) {15 SpringApplication.run(SbSwaggerApplication.class, args);16 log.info("\n----------------------------------------------------------\n\t" +17 "Application demo is running! Access URLs:\n\t" +18 "swagger-ui: \thttp://127.0.0.1:8080/swagger-ui.html\n\t" +19 "----------------------------------------------------------");20 }21 22 }
4、SwaggerConfig配置类
1 package com.dds.sbswagger.config; 2 3 import io.swagger.annotations.ApiOperation; 4 import org.springframework.context.annotation.Bean; 5 import org.springframework.context.annotation.Configuration; 6 import springfox.documentation.builders.PathSelectors; 7 import springfox.documentation.builders.RequestHandlerSelectors; 8 import springfox.documentation.service.ApiInfo; 9 import springfox.documentation.service.Contact;10 import springfox.documentation.spi.DocumentationType;11 import springfox.documentation.spring.web.plugins.Docket;12 import springfox.documentation.swagger2.annotations.EnableSwagger2;13 14 import java.util.Collections;15 16 /**17 * @author DDS18 * @date 2019/9/10 13:5519 */20 @Configuration21 @EnableSwagger222 public class SwaggerConfig {23 @Bean24 public Docket api() {25 return new Docket(DocumentationType.SWAGGER_2)26 .select()27 .apis(RequestHandlerSelectors.basePackage("com.dds.sbswagger.controller"))28 //加了ApiOperation注解的类,才生成接口文档29 .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))30 .paths(PathSelectors.any())31 .build()32 .apiInfo(apiInfo());33 }34 35 private ApiInfo apiInfo() {36 return new ApiInfo(37 "Spring Boot项目集成Swagger实例文档",38 "我的微信公众号:大道七哥,欢迎大家关注。",39 "API V1.0",40 "Terms of service",41 new Contact("大道七哥", "https://www.cnblogs.com/jstarseven/", "jstarseven@163.com"),42 "Apache", "http://www.apache.org/", Collections.emptyList());43 }44 }
5、实体类model
1 package com.dds.sbswagger.model; 2 3 import io.swagger.annotations.ApiModel; 4 import io.swagger.annotations.ApiModelProperty; 5 import lombok.Data; 6 7 /** 8 * @author DDS 9 * @date 2019/9/10 13:5510 */11 @ApiModel("用户实体")12 @Data13 public class User {14 15 /**16 * 用户Id17 */18 @ApiModelProperty("用户id")19 private int id;20 21 /**22 * 用户名23 */24 @ApiModelProperty(value = "用户姓名", example = "zhangdan", required = true)25 private String name;26 27 /**28 * 用户地址29 */30 @ApiModelProperty(value = "用户地址", example = "北京市海淀区", required = true)31 private String address;32 33 /**34 * 用户手机号35 */36 @ApiModelProperty(value = "用户手机号", example = "15689652367", required = true)37 private String phone;38 39 /**40 * 用户年龄41 */42 @ApiModelProperty(value = "用户年龄", example = "24", required = true)43 private Integer age;44 45 }
6、接口开发
1 package com.dds.sbswagger.controller; 2 3 import com.dds.sbswagger.model.User; 4 import io.swagger.annotations.*; 5 import org.springframework.web.bind.annotation.*; 6 7 /** 8 * @author DDS 9 * @date 2019/9/10 13:5510 */11 @RestController12 @RequestMapping("/user")13 @Api(tags = "用户相关接口", description = "提供用户相关的Rest API")14 public class UserController {15 16 @PostMapping("/add")17 @ApiOperation(value = "新增用户接口", notes = "手机号、密码都是必输项,年龄随边填,但必须是数字")18 @ApiImplicitParams({19 @ApiImplicitParam(name = "name", value = "用户名称", required = true, paramType = "form"),20 @ApiImplicitParam(name = "address", value = "用户地址", required = true, paramType = "form"),21 @ApiImplicitParam(name = "phone", value = "用户手机号", required = true, paramType = "form"),22 @ApiImplicitParam(name = "age", value = "用户年龄", required = true, paramType = "form", dataType = "Integer")23 })24 public boolean addUser(@RequestBody User user) {25 return false;26 }27 28 @ApiOperation("通过id查找用户接口")29 @GetMapping("/find/{id}")30 public User findById(@PathVariable("id") int id) {31 return new User();32 }33 34 @ApiOperation("更新用户信息接口")35 @PutMapping("/update")36 @ApiResponses({37 @ApiResponse(code = 400, message = "请求参数没填好"),38 @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对"),39 @ApiResponse(code = 405, message = "未知错误")40 })41 public boolean update(@RequestBody User user) {42 return true;43 }44 45 @ApiOperation("删除用户接口")46 @DeleteMapping("/delete/{id}")47 public boolean delete(@PathVariable("id") int id) {48 return true;49 }50 }
7、swagger界面显示
-END-