Spring Boot笔记(一) springboot 集成 swagger-ui
个人博客网:https://wushaopei.github.io/ (你想要这里多有)
1、添加依赖
<!--SpringBoot整合Swagger-ui-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
2、设置配置类

package com.example.poiutis.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;
/**
* @ClassName SwaggerConfig
* @Description TODO
* @Author wushaopei
* @Date 2019/7/22 16:05
* @Version 1.0
*/
// 启动时加载类
@Configuration
// 启用Swagger API文档
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
// 自行修改为自己的包路径
.apis(RequestHandlerSelectors.basePackage("com.example.poiutis.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("报表管理")
.description("报表管理中心 API 1.0 操作文档")
//服务条款网址
.termsOfServiceUrl("https://blog.csdn.net/weixin_42405670")
.version("1.0")
.contact(new Contact("鮀城小帅", "https://blog.csdn.net/weixin_42405670", "15989746839@163.com"))
.build();
}
}
3、启动类加上注解@EnableSwagger
启动该注解使得用在controller中的swagger注解生效,覆盖的范围下的所有controller

4、Controller 类配置
@Api的使用
API作用在Controller,作为swagger文档资源,该注解将一个controller标注为一个Swagger资源(API). 在默认情况下,Swagger-Core 只会扫描解析具有 @Api 注解的类,而会自动忽略其他类别资源(JAX-RS endpoints、Servlets 等)的注解。
@ApiOperation 的使用
ApiOperation 定义在方法上,描述方法名、方法解释、返回信息、标记等信息。
/**
* 发票管理
* @author issuser
*
*/
@Api(value = "发票",description = "发票操作 API", position = 100, protocols = "http")
@RestController
@RequestMapping(value = "/webcode")
public class InvoiceController {
private static Logger logger = LoggerFactory.getLogger(InvoiceController.class);
@Autowired
invoiceOrderService InvoiceOrderService;
@ApiOperation(value = "导出操作日志到xls文件", notes = "需要登录")
@GetMapping("/exporInvoiceOrderXls")
@ResponseBody
public Object exporInvoiceOrderXls( HttpServletRequest request, HttpServletResponse response,
@RequestParam(value="invoiceOrders")@ApiParam(value="invoiceOrders") List<String> invoiceOrders) {
Map<String, Object> map = new HashMap<>();
response.setContentType("octets/stream");
Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("sheetTitle", "消费记录");
map2.put("header", new String[]{"订单号", "商店名称", "发票批次", "账户名称", "商店地址", "刷卡账号","商店电话","信用卡开户行名称"});
map2.put("fields", new String[]{"invoiceOrder", "companyName", "taxNumber", "accountBank", "companyAddress","bankNumber","companyTelephone","accountName"});
List<InvoiceOrder> invoiceOrderss = InvoiceOrderService.queryInvoiceLists(invoiceOrders,0,9999);
map2.put("data",invoiceOrderss);
List<Map<String, Object>> list = new ArrayList<>();
list.add(map2);
String title = "日常消费刷卡发票记录";
Map<String, short[]> mergedRegion = new HashMap<String, short[]>();
mergedRegion.put("通用标题名称合并", new short[] {0, 0, 0, 100});
ExportExcel<InvoiceOrder> ex = new ExportExcel<>();
// 声明一个工作薄
HSSFWorkbook workbook = new HSSFWorkbook();
// 生成一个标题样式
HSSFCellStyle headerStyle = ex.cellStyle(workbook);
// 生成一个内容样式
HSSFCellStyle textStyle = ex.cellStyle(workbook);
// 生成标题字体
ex.setFont(workbook, headerStyle, (short) 10, HSSFFont.BOLDWEIGHT_NORMAL);
// 生成内容字体
ex.setFont(workbook, textStyle, HSSFFont.BOLDWEIGHT_NORMAL);
try {
OutputStream out = response.getOutputStream();
// 导出文件名称添加当前时间
String date = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
//防止乱码
// response.addHeader("Access-Control-Expose-Headers", "Content-Type,Content-Disposition");
// response.addHeader("Content-Disposition",
// "attachment;filename=" + URLEncoder.encode(title, "GBK") + date
// + ".xls");
response.setHeader("Content-Disposition", "attachment; filename=" + new String((title+".xls").getBytes("GB2312"),"ISO8859-1"));
list.forEach(m -> {
ExcelModel excelModel = new ExcelModel();
excelModel.setWorkbook(workbook);
excelModel.setTitle(title);
excelModel.setFields((String[]) m.get("fields"));
excelModel.setHeader((String[]) m.get("header"));
excelModel.setHeaderRow(2);
excelModel.setMergedRegion(mergedRegion);
excelModel.setHeaderStyle(headerStyle);
excelModel.setTextStyle(textStyle);
excelModel.setTitles((String) m.get("sheetTitle"));
// 生成一页表格
HSSFSheet sheet = workbook.createSheet((String) m.get("sheetTitle"));
excelModel.setSheet(sheet);
ex.exportExcel(excelModel, (Collection<InvoiceOrder>) m.get("data"));
});
try {
workbook.write(out);
} catch (IOException e) {
e.printStackTrace();
}finally {
out.close();
}
map.put("state","0");
map.put("message", "导出成功");
return map;
} catch (IOException e) {
e.printStackTrace();
map.put("state", "1");
map.put("message", e.getMessage());
return map;
}
}
}
| 属性名称 | 备注 |
|---|---|
| value | url 的路径值 |
| tags | 如果设置这个值,value 的值会被覆盖 |
| description | 对 API 资源的描述 |
| produces | For example, "application/json, application/xml" |
| consumes | For example, "application/json, application/xml" |
| protocols | Possible values: http, https, ws, wss |
| authorizations | 高级特性认证时配置 |
| hidden | 配置为 true 将在文档中隐藏 |
| response | 返回的对象 |
| responseContainer | 这些对象是有效的 "List", "Set" or "Map",其他无效 |
| httpMethod | "GET"、"HEAD"、"POST"、"PUT"、"DELETE"、"OPTIONS" and "PATCH" |
| code | http 的状态码 默认 200 |
| extensions | 扩展属性 |
@ApiImplicitParams 和 @ApiImplicitParam 的使用
@ApiImplicitParams 用于描述方法的返回信息,和 @ApiImplicitParam 注解配合使用;@ApiImplicitParam 用来描述具体某一个参数的信息,包括参数的名称、类型、限制等信息。
5、启动项目,进入API列表,localhost/swagger-ui.html#

完整springBoot 整合 Swagger 代码 GitHub 链接地址:
注意问题:
出现以下问题时,要检查SwaggerConfig 中配置的路径是否正确;

即:

6、Swagger常用注解
| 作用范围 | API | 使用位置 |
|---|---|---|
| 协议集描述 | @Api | 用于 Controller 类上 |
| 协议描述 | @ApiOperation | 用在 Controller 的方法上 |
| 非对象参数集 | @ApiImplicitParams | 用在 Controller 的方法上 |
| 非对象参数描述 | @ApiImplicitParam | 用在 @ApiImplicitParams 的方法里边 |
| 响应集 | @ApiResponses | 用在 Controller 的方法上 |
| 响应信息参数 | @ApiResponse | 用在 @ApiResponses 里边 |
| 描述返回对象的意义 | @ApiModel | 用在返回对象类上 |
| 对象属性 | @ApiModelProperty | 用在出入参数对象的字段上 |
Spring Boot笔记(一) springboot 集成 swagger-ui的更多相关文章
- Spring Boot笔记(三) springboot 集成 Quartz 定时任务
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 1. 在 pom.xml 中 添加 Quartz 所需要 的 依赖 <!--定时器 quartz- ...
- Spring Boot笔记(二) springboot 集成 SMTP 发送邮件
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 笔记:乘着项目迭代的间隙,把一些可复用的功能从项目中抽取出来,这是其中之一, 一.添加SMTP 及 MA ...
- Spring Boot笔记(六) springboot 集成 timer 定时任务
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 1.创建具体要执行的任务类: package com.example.poiutis.timer; im ...
- Spring Boot笔记(五) SpringBoot 集成Lombok 插件
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 为了减少代码量,为当前项目添加 lombok 来优雅编码 Lombok 插件安装: a . 添加依赖: ...
- springboot 集成swagger ui
springboot 配置swagger ui 1. 添加依赖 <!-- swagger ui --> <dependency> <groupId>io.sprin ...
- 15、Spring Boot 2.x 集成 Swagger UI
1.15.Spring Boot 2.x 集成 Swagger UI 完整源码: Spring-Boot-Demos 1.15.1 pom文件添加swagger包 <swagger2.versi ...
- spring-boot 集成 swagger 问题的解决
spring-boot 集成 swagger 网上有许多关于 spring boot 集成 swagger 的教程.按照教程去做,发现无法打开接口界面. 项目由 spring mvc 迁移过来,是一个 ...
- SpringBoot集成Swagger,Postman,newman,jenkins自动化测试.
环境:Spring Boot,Swagger,gradle,Postman,newman,jenkins SpringBoot环境搭建. Swagger简介 Swagger 是一款RESTFUL接口的 ...
- springboot集成swagger添加消息头(header请求头信息)
springboot集成swagger上篇文章介绍: https://blog.csdn.net/qiaorui_/article/details/80435488 添加头信息: package co ...
随机推荐
- lambda表达式入门详解
转自 2018-03-02 Sevenvidia 码农翻身 1.什么是Lambda? 我们知道,对于一个Java变量,我们可以赋给其一个"值". 如果你想把"一块代码 ...
- mac下xampp访问php显示403错误
错误描述 New xampp security concept: Access Forbidden Error 403 错误分析和解决 403就是我们访问的时候,被安全策略拒绝了,解决方法 找到文件 ...
- {bzoj2338 [HNOI2011]数矩形 && NBUT 1453 LeBlanc}平面内找最大矩形
思路: 枚举3个点,计算第4个点并判断是否存在,复杂度为O(N3logN)或O(N3α) 考虑矩形的对角线,两条对角线可以构成一个矩形,它们的长度和中点必须完全一样,于是将所有线段按长度和中点排序,那 ...
- [codeforces 200 A Cinema]暴力,优化
题意大致是这样的:有一个有n行.每行m个格子的矩形,每次往指定格子里填石子,如果指定格子里已经填过了,则找到与其曼哈顿距离最小的格子,然后填进去,有多个的时候依次按x.y从小到大排序然后取最小的.输出 ...
- mysql 中 innoDB 与 MySAM
mysql 中 innoDB 与 MyISAM 的特点 --ENGINE = innodb 1.提供事务处理,支持行锁: 2.不加锁读取,增加并发读的用户数量和空间: 3. insert/update ...
- 字符串匹配算法:Sunday算法
背景 我们第一次接触字符串匹配,想到的肯定是直接用2个循环来遍历,这样代码虽然简单,但时间复杂度却是\(Ω(m*n)\),也就是达到了字符串匹配效率的下限.于是后来人经过研究,构造出了著名的KMP算法 ...
- 常用DOS命令大全
常用DOS命令大全 常用的内部命令有MD.CD.RD.DIR.PATH.COPY.TYPE.EDIT.REN.DEL.CLS.VER.DATE.TIME.PROMPT 常用的外部命令有DELTREE. ...
- python-修改文件
1.修改文件1 # fw = open('username','w')# fw.write('hhhh')# fw.flush() #强制把缓冲区里面的数据写到磁盘上1.简单粗暴直接# 1.打开一 ...
- 破坏之王DDoS攻击与防范深度剖析【学习笔记】
一.DDoS初步印象 1.什么是分布式拒绝服务攻击? 1)首先它是一种拒绝服务攻击 我们可以这么认为,凡是导致合法用户不能访问服务的行为,就是拒绝服务攻击. 注:早期的拒绝服务主要基于系统和应用程序的 ...
- 利用js实现 禁用浏览器后退| 去除上一个历史记录链接
也是查找了好多资料才找到的,这种方式,可以消除 后退的所有动作.包括 键盘.鼠标手势等产生的后退动作. <script language="javascript"> / ...