SpringBoot学习笔记(16)----SpringBoot整合Swagger2
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的更多相关文章
- SpringBoot学习笔记(4)----SpringBoot中freemarker、thymeleaf的使用
1. freemarker引擎的使用 如果你使用的是idea或者eclipse中安装了sts插件,那么在新建项目时就可以直接指定试图模板 如图: 勾选freeMarker,此时springboot项目 ...
- springboot学习笔记-5 springboot整合shiro
shiro是一个权限框架,具体的使用可以查看其官网 http://shiro.apache.org/ 它提供了很方便的权限认证和登录的功能. 而springboot作为一个开源框架,必然提供了和sh ...
- springboot学习笔记-6 springboot整合RabbitMQ
一 RabbitMQ的介绍 RabbitMQ是消息中间件的一种,消息中间件即分布式系统中完成消息的发送和接收的基础软件.这些软件有很多,包括ActiveMQ(apache公司的),RocketMQ(阿 ...
- 【转】SpringBoot学习笔记(7) SpringBoot整合Dubbo(使用yml配置)
http://blog.csdn.net/a67474506/article/details/61640548 Dubbo是什么东西我这里就不详细介绍了,自己可以去谷歌 SpringBoot整合Dub ...
- SpringBoot学习笔记(9)----SpringBoot中使用关系型数据库以及事务处理
在实际的运用开发中,跟数据库之间的交互是必不可少的,SpringBoot也提供了两种跟数据库交互的方式. 1. 使用JdbcTemplate 在SpringBoot中提供了JdbcTemplate模板 ...
- SpringBoot学习笔记(6) SpringBoot数据缓存Cache [Guava和Redis实现]
https://blog.csdn.net/a67474506/article/details/52608855 Spring定义了org.springframework.cache.CacheMan ...
- SpringBoot学习笔记(15)----SpringBoot使用Druid
直接访问Druid官网wiki,有详细教程,地址如下: SpringBoot支持Druid地址:https://github.com/alibaba/druid/tree/master/druid-s ...
- SpringBoot学习笔记(11)-----SpringBoot中使用rabbitmq,activemq消息队列和rest服务的调用
1. activemq 首先引入依赖 pom.xml文件 <dependency> <groupId>org.springframework.boot</groupId& ...
- SpringBoot学习笔记(10)-----SpringBoot中使用Redis/Mongodb和缓存Ehcache缓存和redis缓存
1. 使用Redis 在使用redis之前,首先要保证安装或有redis的服务器,接下就是引入redis依赖. pom.xml文件如下 <dependency> <groupId&g ...
随机推荐
- IIS的安装与配置详细图解教程
IIS是Internet Information Services(互联网信息服务)的简称,是有微软公司提供的基于在windows操作系统环境下运行的互联网服务.此处将介绍如何安装配置IIS来构架自己 ...
- StatusBarUtils工具类
import android.app.Activity; import android.app.Dialog; import android.content.Context; import andro ...
- DataReader相关知识点
C#中提供的DataReader可以从数据库中每次提取一条数据. 1. 获取数据的方式[1]DataReader 为在线操作数据, DataReader会一直占用SqlConnection连接,在其获 ...
- python 一句话输出26个英文字母
chr(i) # return i character ord(c) # return integer >>> [chr(i) for i in range(97,123)] ['a ...
- addFooterView(v)与 addHeaderView(v)之后 头或者尾部没有加上去
myExpandableListView.addHeaderView(headView); myExpandableListView.addFooterView(footerView); 原因很简单: ...
- Lucene倒排索引结构及关系
- php基础------将二维数组转三维数组
将二维数组转为三维数组 /** * 二维数组转三维数组(指定键为三维数组的键名) * @param [type] $arr [要排序的数组] * @param [type] $key [指定的键] * ...
- pytorch实战(2)-----回归例子
一.回归任务介绍: 拟合一个二元函数 y = x ^ 2. 二.步骤: 导入包 创建数据 构建网络 设置优化器和损失函数 前向和后向传播训练网络 画图 三.代码: 导入包: import torch ...
- 使用ajax实现搜索功能
最近要做一个搜索功能,网上搜了一圈,终于做出来了,很简单的一个,这里分享我的方法,希望对大家有用,不足之处还请指教. 这里使用ajax提交数据,配合jquery将数据显示出来. 用jq的keyup ...
- [USACO18JAN] Lifeguards S (线段树:扫描线面积)
扫描线裸题没什么好说的 注意空间不要开小了!!! #include <cstdio> #include <cstring> #include <algorithm> ...