SpringBoot+Swagger2四步整合

第一步:添加相关依赖

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>1.5.9.RELEASE</version>
  5. </parent>
  6. <properties>
  7. <swagger2.version>2.7.0</swagger2.version>
  8. </properties>
  9. <dependencies>
  10. <!--springBoot 相关依赖-->
  11. <dependency>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-web</artifactId>
  14. </dependency>
  15. <!--swagger2 相关依赖-->
  16. <dependency>
  17. <groupId>io.springfox</groupId>
  18. <artifactId>springfox-swagger2</artifactId>
  19. <version>${swagger2.version}</version>
  20. </dependency>
  21. <dependency>
  22. <groupId>io.springfox</groupId>
  23. <artifactId>springfox-swagger-ui</artifactId>
  24. <version>${swagger2.version}</version>
  25. </dependency>
  26. </dependencies>

创建SrpingBoot启动类

  1. /**
  2. * Springboot+Swagger整合启动类
  3. *
  4. * @author Y.yang
  5. * @date 2019/3/12
  6. */
  7. @SpringBootApplication
  8. public class SwaggerApplication {
  9. public static void main(String[] args) {
  10. SpringApplication.run(SwaggerApplication.class, args);
  11. }
  12. }

第二步:配置Swagger2

注意添加@Configuration EnableSwagger2注解

  1. /**
  2. * Swagger2 接口Api文档 配置文件
  3. *
  4. * @author Y.yang
  5. * @date 2019/3/12
  6. */
  7. @Configuration
  8. @EnableSwagger2
  9. public class SwaggerConfig {
  10. /**
  11. * 初始化创建Swagger Api
  12. */
  13. @Bean
  14. public Docket createRestApi() {
  15. return new Docket(DocumentationType.SWAGGER_2)
  16. // 详细信息定制
  17. .apiInfo(apiInfo())
  18. .select()
  19. // 指定当前包路径
  20. .apis(RequestHandlerSelectors.basePackage("com.fame.controller"))
  21. // 扫描所有 .apis(RequestHandlerSelectors.any())
  22. .paths(PathSelectors.any())
  23. .build();
  24. }
  25. /**
  26. * 添加摘要信息
  27. */
  28. private ApiInfo apiInfo() {
  29. // 用ApiInfoBuilder进行定制
  30. return new ApiInfoBuilder()
  31. .title("标题:springBoot-Swagger2整合学习")
  32. .description("描述:文档构建器")
  33. .contact(new Contact("Fame-springBoot-Swagger2", null, null))
  34. .version("版本号: 1.0")
  35. .build();
  36. }
  37. }

springfox为我们提供了一个Docket(摘要的意思)类,我们需要把它做成一个Bean注入到spring中,

显然,我们需要一个配置文件,并通过一种方式(显然它会是一个注解)告诉程序,这是一个Swagger配置文件。

springfox允许我们将信息组合成一个ApiInfo的类,作为构造参数传给Docket(当然也可以不构造这个类,而直接使用null,但是你的这个API就太low了)。

第三步:创建测试实例

  1. /**
  2. * Swagger接口测试
  3. *
  4. * @author Y.yang
  5. * @date 2019/3/12
  6. */
  7. @RestController
  8. public class UserController {
  9. @GetMapping("/get")
  10. public String get(){
  11. return "Hello Swagger2";
  12. }
  13. }

第四步:输入SwaggerUI地址

http://localhost:8080/swagger-ui.html

Swagger2基本使用-常用注解

接口/方法常用注解

  1. /**
  2. * Swagger接口测试
  3. *
  4. * @author Y.yang
  5. * @date 2019/3/12
  6. */
  7. @Api(value = "用户信息", tags = { "用户信息" })
  8. @RestController
  9. public class UserController {
  10. @ApiOperation(value = "用户信息分页查询")
  11. @GetMapping("/page")
  12. public String page(User user) {
  13. return "Hello Swagger2";
  14. }
  15. @ApiOperation(value = "用户信息查询")
  16. @ApiImplicitParams({
  17. @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Integer"),
  18. @ApiImplicitParam(name = "file", value = "文件导入", required = true, dataType = "MultipartFile")
  19. })
  20. @GetMapping("/id")
  21. public UserVo getUser(Long id, MultipartFile file) {
  22. return new UserVo();
  23. }
  24. }

@Api: 描述类/接口的主要用途

用于类;表示标识这个类是swagger的资源

tags–表示说明

value–也是说明,不会显示在接口文档上,可以使用tags替代

但是tags如果有多个值,会生成多个list

  1. @Api(value = "用户信息", tags = { "用户信息" })

@ApiOperation: 描述方法用途

  1. @ApiOperation(value = "用户信息分页查询")

@ApiImplicitParam: 描述方法的参数

@ApiImplicitParams: 描述方法的参数(Multi-Params)

  1. @ApiImplicitParams({
  2. @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Integer"),
  3. @ApiImplicitParam(name = "file", value = "文件导入", dataType = "MultipartFile")
  4. })

实体类常用注解

  1. /**
  2. * 用户信息 数据传输对象 Dto(Data Transfer Object)
  3. *
  4. * @author Y.yang
  5. * @date 2019/3/29
  6. */
  7. @ApiModel(description = "用户信息请求对象")
  8. @Data
  9. public class User implements Serializable {
  10. private static final long serialVersionUID = -6986638131456347054L;
  11. @ApiModelProperty(value = "姓名")
  12. private String username;
  13. @ApiModelProperty(value = "性别")
  14. private String sex;
  15. @ApiModelProperty(value = "年龄")
  16. private Integer age;
  17. }

@ApiModel:描述实体类(Dto、Vo、Do等)

  1. @ApiModel(description = "用户信息请求对象")

@ApiModelProperty:描述实体类的字段

  1. @ApiModelProperty(value = "姓名")

SpringBoot+Swagger2 整合的更多相关文章

  1. SpringBoot与Swagger2整合

    一.Swagger简介与优势 相信各位在公司写API文档数量应该不少,当然如果你还处在自己一个人开发前后台的年代,当我没说,如今为了前后台更好的对接,还为了以后交接方便,都有要求写API文档. Swa ...

  2. SpringBoot+Swagger整合API

    SpringBoot+Swagger整合API Swagger:整合规范的api,有界面的操作,测试 1.在pom.xml加入swagger依赖 <!--整合Swagger2配置类--> ...

  3. 学习SpringBoot,整合全网各种优秀资源,SpringBoot基础,中间件,优质项目,博客资源等,仅供个人学习SpringBoot使用

    学习SpringBoot,整合全网各种优秀资源,SpringBoot基础,中间件,优质项目,博客资源等,仅供个人学习SpringBoot使用 一.SpringBoot系列教程 二.SpringBoot ...

  4. SpringMVC中使用Swagger2整合

    Swagger2是什么 Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件. Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 W ...

  5. springboot+swagger2

    springboot+swagger2 小序 新公司的第二个项目,是一个配置管理终端机(比如:自动售卖机,银行取款机)的web项目,之前写过一个分模块的springboot框架,就在那个框架基础上进行 ...

  6. SpringBoot 同时整合thymeleaf html、vue html和jsp

    问题描述 SpringBoot如何同时访问html和jsp SpringBoot访问html页面可以,访问jsp页面报错 SpringBoot如何同时整合thymeleaf html.vue html ...

  7. SpringBoot+AOP整合

    SpringBoot+AOP整合 https://blog.csdn.net/lmb55/article/details/82470388 https://www.cnblogs.com/onlyma ...

  8. SpringBoot+Redis整合

    SpringBoot+Redis整合 1.在pom.xml添加Redis依赖 <!--整合Redis--> <dependency> <groupId>org.sp ...

  9. springboot+maven整合spring security

    springboot+maven整合spring security已经做了两次了,然而还是不太熟悉,这里针对后台简单记录一下需要做哪些事情,具体的步骤怎么操作网上都有,不再赘述.1.pom.xml中添 ...

随机推荐

  1. PaaS服务之路漫谈(三)

    此文已由作者尧飘海授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. Monolithic架构在产品访问量很大的情况下,有可能常会导致整个产品迭代或升级过程不能按预期进行,或者上 ...

  2. 【学习笔记】dsu on tree

    我也不知道为啥这要起这名,完完全全没看到并查集的影子啊…… 实际上原理就是一个树上的启发式合并. 特点是可以在$O(nlogn)$的时间复杂度内完成对无修改的子树的统计,复杂度优于莫队算法. 局限性也 ...

  3. [Python]Threading.Thread之Daemon线程

    之前对Daemon线程理解有偏差,特记录说明: 一.什么是Daemon A thread can be flagged as a "daemon thread". The sign ...

  4. Redis 的 Sentinel

    Redis 的 Sentinel 系统用于管理多个 Redis 服务器(instance), 该系统执行以下三个任务: 监控(Monitoring): Sentinel 会不断地检查你的主服务器和从服 ...

  5. CAN总线协议 学习笔记

    1.CAN总线网络 CAN总线网络主要挂在CAN_H和CAN_L,各个节点通过这两条线实现信号的串行差分传输,为了避免信号的反射和干扰,还需要在CAN_H和CAN_L之间接上120欧姆的终端电阻,但是 ...

  6. 《JAVA与模式》之合成模式

    在阎宏博士的<JAVA与模式>一书中开头是这样描述合成(Composite)模式的: 合成模式属于对象的结构模式,有时又叫做“部分——整体”模式.合成模式将对象组织到树结构中,可以用来描述 ...

  7. Web应用配置虚拟主机(www.baidu.com)

    最终效果:浏览器地址栏输入www.baidu.com访问时,会显示自己的网页 1.创建文件 任意盘新建一个www.baidu.com文件,在该文件夹下新建WEB-INF文件.自己写的一个html文件, ...

  8. 【JXOI2018】排序问题 贪心

    我们令$sum_i$表示数字i在加完数字的数列中出现的次数,那么答案显然为$\dfrac{(n+m)!}{\sum_{i=0}^{\infty}sum_i!}$ 不难发现,当每次添加的数为$[l,r] ...

  9. 基于CAS操作的非阻塞算法

    非阻塞算法(non-blocking algorithms)定义        所谓非阻塞算法是相对于锁机制而言的,是指:一个线程的失败或挂起不应该引起另一个线程的失败或挂起的一种算法.一般是利用硬件 ...

  10. Kafka消息队列

    转自:http://blog.csdn.net/yfkiss/article/details/17348693 代码案例 http://blog.csdn.net/ganglia/article/de ...