SpringBoot-整合Swagger2
swagger2是一个用于生成、并能直接调用的可是话restful风格的服务
下面贴出springboot整合swagger2代码
一、maven依赖
这里使用的spring-boot版本是2.1.1.RELEASE,swagger2使用的是2.9.2,我一开始用的springboot1.5.6.RELEASE,swagger从2.4到2.9.2都用了,结果又很多jar包冲突,springboot换成了2.*版本依旧,依然有jar冲突,无奈,只能把冲突的依赖找出来,有具体报错信息,把他exclude掉就行,而且idea的maven-helper插件帮助也蛮大
<!-- swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
<exclusions>
<exclusion>
<artifactId>spring-context</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
<exclusion>
<artifactId>spring-beans</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
<exclusion>
<artifactId>spring-aop</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
二、swagger2配置
package com.hy.other.config; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
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; @Configuration
@EnableSwagger2
public class SwaggerConfiguration { @Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
// 生成api的包路径(一般是我们controller包的路径)
.apis(RequestHandlerSelectors.basePackage("com.hy.other.controller"))
.paths(PathSelectors.any())
.build();
} // swagger ui 页面里面的一些信息
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
// 页面标题
.title("spring boot swagger")
// 创建人
.contact(new Contact("hy", "https://www.cnblogs.com/xhy-shine", ""))
// 版本号
.version("1.0")
// 描述
.description("API接口文档")
.build();
}
}
注意:@EnableSwagger2一定要加上,不然会无法访问swagger的ui界面,会报如下错误

三、代码(主要是swagger2的注解)
注解有挺多的,比较常用的就是@Api、@ApiOperation、@ApiImplicitParam、@ApiModel、@ApiModelProperty
@Api:包括下面的所有接口,有点类注释的意思
@ApiOperation:给接口增加说明
@ApiImplicitParams、@ApiImplicitParam:给接口参数添加说明
@ApiModel:描述对象类型的请求参数
@ApiModelProperty:描述对象的属性
注意:@ApiModelProperty注解的参数说明
paramType:指定参数放在哪个地方(header:request header中,使用@RequestHeader获取;query:使用@RequestParam获取;path:使用@PathVariable获取;body、form两者不常用)可参考:https://swagger.io/docs/specification/describing-parameters/
name:参数名
dataType:参数类型
required:是否必须
value:参数的意思
defaultValue:默认值
package com.hy.other.controller; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; @Api(description = "MQ接口")
@RestController
@RequestMapping("/messageQueue")
public class MessageQueueController { @Value("${rabbitmq.exchangeName}")
private String exchangeName;
@Value("${rabbitmq.queueName}")
private String queueName;
@Value("${rabbitmq.routeKey}")
private String routeKey; @Autowired
private RabbitTemplate rabbitTemplate; @ApiOperation(value = "send message to spin", notes = "发送消息到spin")
@ApiImplicitParam(name = "message", value = "消息内容", required = true, paramType = "query")
@RequestMapping("/sendWso2ToSpin")
public String sendWso2ToSpin(@RequestParam("message") String message) {
rabbitTemplate.convertAndSend(exchangeName, routeKey, message);
return message;
} }
访问地址你的项目根路径加上swagger-ui.html就行


1、swagger会根据@RequestParam、@RequestBody注解来决定用form提交还是application/json格式(上图出现Parameter content type为application/json是因为我把请求参数前的注解改成了@RequestBody)
2、会根据@RequestMapping、@PostMapping、@GetMapping等注解生成对应的请求,如上,我写的@RequestMapping,他把所有请求格式都生成了
3、如果接收的是对象,可以使用@ApiModel结合@ApiModelProperty
SpringBoot-整合Swagger2的更多相关文章
- SpringBoot(七):SpringBoot整合Swagger2
原文地址:https://blog.csdn.net/saytime/article/details/74937664 手写Api文档的几个痛点: 文档需要更新的时候,需要再次发送一份给前端,也就是文 ...
- SpringBoot整合Swagger2
相信各位在公司写API文档数量应该不少,当然如果你还处在自己一个人开发前后台的年代,当我没说,如今为了前后台更好的对接,还是为了以后交接方便,都有要求写API文档. 手写Api文档的几个痛点: 文档需 ...
- SpringBoot整合Swagger2(Demo示例)
写在前面 由于公司项目采用前后端分离,维护接口文档基本上是必不可少的工作.一个理想的状态是设计好后,接口文档发给前端和后端,大伙按照既定的规则各自开发,开发好了对接上了就可以上线了.当然这是一种非常理 ...
- springboot 整合Swagger2的使用
Swagger2相较于传统Api文档的优点 手写Api文档的几个痛点: 文档需要更新的时候,需要再次发送一份给前端,也就是文档更新交流不及时. 接口返回结果不明确 不能直接在线测试接口,通常需要使用工 ...
- SpringBoot整合Swagger2案例,以及报错:java.lang.NumberFormatException: For input string: ""原因和解决办法
原文链接:https://blog.csdn.net/weixin_43724369/article/details/89341949 SpringBoot整合Swagger2案例 先说SpringB ...
- SpringBoot整合Swagger2详细教程
1. 简介 随着前后端分离开发模式越来越流行,编写接口文档变成了开发人员非常头疼的事.而Swagger是一个规范且完整的web框架,用于生成.描述.调用可视化的RESTful风格的在线接口文档,并 ...
- SpringBoot整合Swagger2及使用
简介 swagger是一个流行的API开发框架,这个框架以"开放API声明"(OpenAPI Specification,OAS)为基础, 对整个API的开发周期都提供了相应的解决 ...
- SpringBoot整合Swagger2,再也不用维护接口文档了!
前后端分离后,维护接口文档基本上是必不可少的工作.一个理想的状态是设计好后,接口文档发给前端和后端,大伙按照既定的规则各自开发,开发好了对接上了就可以上线了.当然这是一种非常理想的状态,实际开发中却很 ...
- SpringBoot学习笔记(16)----SpringBoot整合Swagger2
Swagger 是一个规范和完整的框架,用于生成,描述,调用和可视化RESTful风格的web服务 http://swagger.io Springfox的前身是swagger-springmvc,是 ...
- Spring Boot2 系列教程(十七)SpringBoot 整合 Swagger2
前后端分离后,维护接口文档基本上是必不可少的工作. 一个理想的状态是设计好后,接口文档发给前端和后端,大伙按照既定的规则各自开发,开发好了对接上了就可以上线了.当然这是一种非常理想的状态,实际开发中却 ...
随机推荐
- 小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_5-7.授权登录获取微信用户个人信息实战
笔记 7.授权登录获取微信用户个人信息实战 简介:讲解使用授权码code获取用户个人信息接口 关键点:看微信文档,字段尽量用拷贝 1.通过code获取access_token ...
- dede 友情链接显示不全解决方法
只需在html页面中调用友情链接的地方修改链接长度即可 titlelen: 链接文字长度(修改此数值即可) row: 友情链接的数量 {dede:flink titlelen="50&quo ...
- python中学习K-Means和图片压缩
python中学习K-Means和图片压缩 大家在学习python中,经常会使用到K-Means和图片压缩的,我们在此给大家分享一下K-Means和图片压缩的方法和原理,喜欢的朋友收藏一下吧. 通俗的 ...
- Go项目实战:打造高并发日志采集系统(一)
项目结构 本系列文章意在记录如何搭建一个高可用的日志采集系统,实际项目中会有多个日志文件分布在服务器各个文件夹,这些日志记录了不同的功能.随着业务的增多,日志文件也再增多,企业中常常需要实现一个独立的 ...
- 数组中存放model去重
在这个项目中出现"添加model数据"数组重复的情况,这就涉及到数组去重的问题了...... 1. 一开始使用的最笨的方法: 依次循环两个数组(原有的数组,选择的数组),双重for ...
- Redis Guide
1. Redis简介 Redis是一个开源(BSD许可),内存存储的数据结构服务器,可用作数据库,高速缓存和消息队列代理.它支持字符串.哈希表.列表.集合.有序集合,位图,hyperloglogs等数 ...
- antd <BackTop>组件的使用
<Content className={style.content} style={{ maxHeight: 'calc(100vh - 175px)',overflowY:"auto ...
- Django:(02)项目配置
上一篇我们创建了一个Django项目,并且让它运行了起来了. 当是,我们还没有使用到我们创建的应用,以及templates模版目录. 需求: 在此之前我们根据需要对我们的项目进行配置修改. 在项目开发 ...
- STS中依赖项的设置
经过试验,把依赖项总结一下,可能会不断修改. 1. 父依赖项(固定) <parent> <groupId>org.springframework.boot</groupI ...
- LeetCode.1184-公交车站之间的距离(Distance Between Bus Stops)
这是小川的第次更新,第篇原创 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第265题(顺位题号是1184).公交车有n个从0到n-1的车站,形成一个圆圈.我们知道所有相邻车站对之间的 ...