Knife4j框架的使用
文章目录
- 引入依赖
- 配置Knife4j
- 使用Knife4j
Knife4j 是基于 Swagger 的增强工具,对 Swagger 进行了拓展和优化,从而有更美观的界面设计和更强的功能
引入依赖
Spring Boot 2.7.18 版本
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-openapi3-spring-boot-starter</artifactId>
<version>4.5.0</version>
</dependency>
配置Knife4j
● Knife4j 的文档(/doc.html )是通过 动态扫描你的 Spring Boot 项目中的 @RestController、@RequestMapping 等注解 自动生成的。
● 它不会额外启动一个独立的 HTTP 服务,而是 嵌入在你的 Spring Boot 应用中。因此访问 http://localhost:8080/doc.html 时,请求会被的 Spring Boot 应用处理,并返回动态生成的 HTML 页面。
● 这里 springboot 的端口是 9999,因此我们访问的默认地址是 http://localhost:9999/doc.html
application.yml 文件中配置
server:port: 9999######## knife4j的配置 默认地址: http://localhost:9999/doc.html#########
knife4j:# 开启增强配置enable: true# 开启Swagger的Basic认证功能,默认是falsebasic:enable: true# Basic认证用户名username: test# Basic认证密码password: 123
使用Knife4j
- @Tag(name = “导出文件到网页”) 一级目录
- @Operation(summary = “导出Excel文件到网页”) 二级目录
@Controller
@RequestMapping(value = "/test")
@Slf4j
@Tag(name = "导出文件到网页")
public class GenerateExcelToWebWithPOIController {@ResponseBody@Operation(summary = "导出Excel文件到网页")@GetMapping("/exportExcel")public void exportExcel(HttpServletResponse response, @RequestParam(value = "name") String name) throws Exception {String[] tableHeaders = {"id", "姓名", "年龄"};// 使用 XSSFWorkbook 处理 .xlsx 文件Workbook workbook = new XSSFWorkbook();Sheet sheet = workbook.createSheet("Sheet1");CellStyle cellStyle = workbook.createCellStyle();// 创建表头Row headerRow = sheet.createRow(0);for (int i = 0; i < tableHeaders.length; i++) {Cell cell = headerRow.createCell(i);cell.setCellValue(tableHeaders[i]);cell.setCellStyle(cellStyle); // 应用样式(可选)}// 这里可以添加数据行的逻辑, 根据需要填充数据/*Row dataRow = sheet.createRow(1);dataRow.createCell(0).setCellValue(1); // iddataRow.createCell(1).setCellValue("张三"); // 姓名dataRow.createCell(2).setCellValue(25); // 年龄*/// 获取到response的输出流OutputStream outputStream = response.getOutputStream();// 重置response,以避免冲突response.reset();// 设置响应的内容类型为"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");// 设置响应头 Content-disposition 为 attachment;filename=template.xlsxresponse.setHeader("Content-disposition", "attachment;filename=template.xlsx");// 写入工作簿到响应输出流workbook.write(outputStream);outputStream.flush();outputStream.close();workbook.close(); // 关闭工作簿log.info("导出一次excel文件到桌面");}@ResponseBody@Operation(summary = "导出文本文件到网页")@GetMapping("/exportTxt")public ResponseEntity<Void> exportTxt(HttpServletResponse response, @RequestParam(value = "name") String name) throws Exception {String fileName = "导出的文本文件.txt";// 设置响应头,指定返回的是文本文件response.setContentType("text/plain; charset=UTF-8");response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));// 写入文本内容到响应流PrintWriter writer = response.getWriter();try{writer.write("这是一个导出的文本");return ResponseEntity.ok().build();}catch (Exception e){log.error("导出文件失败:" + e);return ResponseEntity.internalServerError().build();}finally {writer.flush();writer.close();}}}
访问 SpringBoot 生成的文档
[http://localhost:9999/doc.html](http://localhost:9999/doc.html)