Swagger 是一个规范和完整的框架,用于生成,描述,调用和可视化RESTful风格的web服务

  http://swagger.io

  Springfox的前身是swagger-springmvc,是一个开源的API doc框架,可以将我们的Controller接口的方法以文档的形式展现,基于swagger,这样就方便开发人员不用在开发完接口服务之后还需要手写一份文档给需要的人。

  使用方式如下

  引入依赖:

  pom.xml文件

<dependency><groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.6.1</version> </dependency>

  编写配置类,并使用@EnableSwagger2开启支持swagger2,配置类如下

  

package com.wangx.boot.util;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; import static springfox.documentation.builders.PathSelectors.regex; @Configuration @EnableSwagger2 public class Swagger2Configuration { @Bean public Docket accessToker() { return new Docket(DocumentationType.SWAGGER_2). groupName("api")//定一组 .select()//选择那些路径和接口api会生成document .apis(RequestHandlerSelectors.basePackage(""))//拦截的包 .paths(regex("/api/.*"))//拦截该路劲下的接口 .build() //创建          .apiInfo(apiInfo()); //配置说明 } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("测试项目")//标题 .description("springboot整合swagger")//描述 .termsOfServiceUrl("http://www.baidu.com")// .contact(new Contact("wangx", "http://baidu.com","1028106567@qq.com"))//联系人 .version("1.0")//版本 .build(); } }

  这样就会拦截api路劲下的所有接口并生成document.

  访问:http://localhost:8080/swagger-ui.html#/

  视图如下:

  

  可以查看接口的相关信息,并且也可以不用通过postman就可以测试接口了。相当的便利的。

  同时swagger也提供了一些注解可以让我们使用在类或者方法上

  Swagger的注解及作用。 

  @Api:修饰整个类,描述Controller的作用
  @ApiOperation:描述一个类的一个方法,或者说一个接口
  @ApiParam:单个参数描述
  @ApiModel:用对象来接收参数
  @ApiProperty:用对象接收参数时,描述对象的一个字段
  @ApiResponse:HTTP响应其中1个描述
  @ApiResponses:HTTP响应整体描述
  @ApiIgnore:使用该注解忽略这个API
  @ApiError :发生错误返回的信息
  @ApiImplicitParam:一个请求参数
  @ApiImplicitParams:多个请求参数

原文 SpringBoot学习笔记(16)----SpringBoot整合Swagger2

转载于:https://www.cnblogs.com/xiaoshen666/p/10844089.html

SpringBoot学习笔记(16)----SpringBoot整合Swagger2相关推荐

  1. SpringBoot学习笔记(4)----SpringBoot中freemarker、thymeleaf的使用

    1. freemarker引擎的使用 如果你使用的是idea或者eclipse中安装了sts插件,那么在新建项目时就可以直接指定试图模板 如图: 勾选freeMarker,此时springboot项目 ...

  2. SpringBoot学习笔记(9)----SpringBoot中使用关系型数据库以及事务处理

    在实际的运用开发中,跟数据库之间的交互是必不可少的,SpringBoot也提供了两种跟数据库交互的方式. 1. 使用JdbcTemplate 在SpringBoot中提供了JdbcTemplate模板 ...

  3. 【Springboot学习笔记】SpringBoot+Mybatis+Thymeleaf+Layui数据表单从零开始实现按条件模糊分页查询的方法

    [Springboot学习笔记]SpringBoot+Mybatis+Thymeleaf+Layui数据表单从零开始实现按条件模糊分页查询的方法 目录 1.搭建环境 1.1直接从网上下载SpringB ...

  4. springboot学习笔记-5 springboot整合shiro

    shiro是一个权限框架,具体的使用可以查看其官网 http://shiro.apache.org/  它提供了很方便的权限认证和登录的功能. 而springboot作为一个开源框架,必然提供了和sh ...

  5. SpringBoot 学习笔记

    SpringBoot 学习笔记 文章目录 SpringBoot 学习笔记 1. SpringBoot简介 1.1 什么是Spring 1.2 Spring 是如何简化Java开发的 1.3 什么是 S ...

  6. SpringBoot学习笔记(16):单元测试

    SpringBoot学习笔记(16):单元测试 单元测试 单元测试(英语:Unit Testing)又称为模块测试,是针对程序模块(软件设计的最小单位)来进行正确性检验的测试工作.程序单元是应用的最小 ...

  7. 2.6 springBoot学习笔记(2.6)——- 整合mybatisPlus

    更多文章 更多系列文章在个人网站 springBoot学习系列笔记文章 springBoot学习笔记(1)-- 搭建springBoot项目 文章目录 更多文章 springBoot学习系列笔记文章 ...

  8. SpringBoot(学习笔记)

    SpringBoot学习笔记 从今天开始就进入微服务阶段 一些小问题 1.HelloWorld 1.1回顾什么是Spring 1.2什么是SpringBoot 1.3微服务架构 2.第一个Spring ...

  9. SpringBoot学习笔记(3):静态资源处理

    SpringBoot学习笔记(3):静态资源处理 在web开发中,静态资源的访问是必不可少的,如:Html.图片.js.css 等资源的访问. Spring Boot 对静态资源访问提供了很好的支持, ...

最新文章

  1. RESTful之分页Pagination
  2. 前端性能测试详细分析(二)
  3. 安装python步骤-小白入门:Python安装的10个步骤,极其细致!!
  4. Android Studio中安装OpenCV SDK
  5. Qt修炼手册8_常用的容器类QVector和QList
  6. (*长期更新)软考网络工程师学习笔记——Section 15 无线网络技术
  7. jq之fadeIn()
  8. 【转】硬盘的寻址和工作模式
  9. 理解和使用NT驱动程序的执行上下文
  10. Xshell中文乱码问题
  11. 快速ping_PING你真的会用么?
  12. Oracle数据泵的使用
  13. PDFelement如何快速地将文本添加到PDF?
  14. SpringBoot启动报错(一篇教你如何处理mapper绑定问题) BindingException: Invalid bound statement (not found)
  15. python定时发送qq消息
  16. 百度硬盘搜索安装步骤
  17. 文本匹配、文本相似度模型之BIMPM
  18. 基于ssm的搬家管理系统
  19. webpack源码分析之五:sourcemap
  20. PF_RING安装与使用总结

热门文章

  1. 每日一题(50)—— 各类型与零值的比较
  2. 【Pytorch神经网络基础理论篇】 03 数据操作 + 数据预处理
  3. 【Pytorch神经网络实战案例】01 CIFAR-10数据集:Pytorch使用GPU训练CNN模版-方法①
  4. 手机长曝光怎么设置_摄影教程丨手机如何拍摄长曝光照片,流光快门,星空银河搞起来!...
  5. python中continue格式_python自学(3)--for 、 while、 break、continue、字符格式化输出...
  6. LeetCode 2039. 网络空闲的时刻(BFS)
  7. 词云(WordCloud)制作
  8. 程序员面试金典 - 面试题 08.02. 迷路的机器人(DFS/动态规划)
  9. LeetCode 287. 寻找重复数(BitMap)
  10. tcp长连接和短连接的区别_TCP --- 连接