Swagger转换成Excel文件

1、添加swagger解析依赖包:

xml 复制代码
        <dependency>
            <groupId>io.swagger.parser.v3</groupId>
            <artifactId>swagger-parser</artifactId>
            <version>2.1.12</version>
        </dependency>

2、示例代码:

java 复制代码
package com.rlcloud.risk.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.models.*;
import io.swagger.models.parameters.Parameter;
import io.swagger.models.properties.Property;
import io.swagger.parser.SwaggerParser;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Map;


/**
 * @Description : Swagger转换成Excel文件
 * @date 2024/3/28 17:30
 * @return
 * @auther xushuanglu
 */
public class SwaggerToExcel {

    public static void main(String[] args) throws Exception {
        // 1、获取Swagger文档
        Swagger swagger = new SwaggerParser().read("http://your-swagger-url/v2/api-docs");

        // 创建Excel工作簿
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("Swagger API Documentation");

        // 创建标题行
        Row titleRow = sheet.createRow(0);
        Cell titleCell1 = titleRow.createCell(0);
        titleCell1.setCellValue("Path");
        Cell titleCell2 = titleRow.createCell(1);
        titleCell2.setCellValue("Description");
        // ... 添加其他需要的列标题
        Cell titleCell3 = titleRow.createCell(2);
        titleCell3.setCellValue("dataType");
        Cell titleCell4 = titleRow.createCell(3);
        titleCell4.setCellValue("接口名称");
        Cell titleCell5 = titleRow.createCell(4);
        titleCell5.setCellValue("code");

        int rowNum = 1;
        String oldNum = "001";
        // 遍历每个路径
        for (String path : swagger.getPaths().keySet()) {
            // 遍历每个操作
            for (Map.Entry<HttpMethod, Operation> entry : swagger.getPaths().get(path).getOperationMap().entrySet()) {
                Row row = sheet.createRow(rowNum);
                Cell cell1 = row.createCell(0);
                cell1.setCellValue(path);
                Cell cell2 = row.createCell(1);
                cell2.setCellValue(entry.getValue().getSummary());
                // ... 填充其他需要的信息
                Cell cell3 = row.createCell(2);
                //获取请求参数
                List<Parameter> parameters = entry.getValue().getParameters();

                parameters.stream().forEach(param -> {

                    //请求参数
                    System.out.println("----------------------请求参数开始---------------------------");
                    //请求参数
                    String name = param.getName();
                    String in = param.getIn();
                    //判断请求参数方式
                    if("body".equals(in)){
                        System.out.println(name + "  : 请求参数是body------------------------------");
                        String capitalized = StrUtil.capitalizeFirstLetter(name); // 首字母大写
                        System.out.println("首字母大写转换:  " + capitalized);

                        String replaceStr = capitalized.replace("Entity", "");
                        System.out.println("去掉Entity字符转换:  " + replaceStr);

                        //获取实体列表
                        Map<String, Model> definitions = swagger.getDefinitions();
                        for (String key : definitions.keySet()) {
                            Model value = definitions.get(key);
                            //判断实体名称是否匹配如果匹配,打印字段信息
                            if(replaceStr.equals(value.getTitle())){
                                // 处理key和value
                                System.out.println("----------------------开始---------------------------");
                                System.out.println("实体:   " + value.getTitle());
                                Map<String, Property> properties = value.getProperties();
                                //遍历实体字段
                                properties.forEach((key1, value1) -> {
                                    // 字段值和字段描述:例如:projectName: 项目名称
                                    System.out.println("字段:" + key1 + ":" + "  字段描述:  " + value1.getDescription());
                                });
                                System.out.println("----------------------结束---------------------------");

                                // 创建ObjectMapper实例
                                ObjectMapper mapper = new ObjectMapper();

                                // 将Map转换为JSON字符串
                                String jsonString = null;
                                try {
                                    jsonString = mapper.writeValueAsString(properties);
                                } catch (JsonProcessingException e) {
                                    e.printStackTrace();
                                }

                                // 打印JSON字符串
                                System.out.println(jsonString);

                                cell3.setCellValue(jsonString);

                            }
                        }
                    }

                    String description = param.getDescription();
                    boolean required = param.getRequired();
                    System.out.println("name: " + name + "   in: " + in + " || required: " + required + " || description: " + description);
                    System.out.println("----------------------请求参数结束---------------------------");

                });

                Cell cell4 = row.createCell(3);
                cell4.setCellValue(entry.getValue().getTags().get(0) + "-" + entry.getValue().getSummary());

                Cell cell5 = row.createCell(4);
                cell5.setCellValue(oldNum = String.format("RMS" + "%03d",rowNum));

                rowNum++;
            }
        }

        // 导出Excel文件
        try (FileOutputStream outputStream = new FileOutputStream("swagger_system.xlsx")) {
            workbook.write(outputStream);
        }
    }

}

3、打印信息样例

java 复制代码
----------------------请求参数开始---------------------------
collectionTaskCommitRecordEntity  : 请求参数是body------------------------------
首字母大写转换:  CollectionTaskCommitRecordEntity
去掉Entity字符转换:  CollectionTaskCommitRecord
----------------------开始---------------------------
实体:   CollectionTaskCommitRecord
字段:collectContent:  字段描述:   采集内容
字段:collectEndTime:  字段描述:   采集结束时间
字段:collectStartTime:  字段描述:   采集开始时间
字段:collectionTaskId:  字段描述:   信息采集任务id
字段:createBy:  字段描述:  null
字段:createTime:  字段描述:  null
字段:dataRetrieval:  字段描述:   资料调阅
字段:id:  字段描述:  null
字段:informationCollectionId:  字段描述:   信息采集id
字段:isArchived:  字段描述:  null
字段:projectName:  字段描述:   项目名称
字段:relatedMaterials:  字段描述:   相关材料
字段:relatedMaterialsUrl:  字段描述:   相关材料url
字段:remark:  字段描述:   备注
字段:status:  字段描述:   状态
字段:type:  字段描述:   提交类型 1:提交 2:追加提交
字段:updateBy:  字段描述:  null
字段:updateTime:  字段描述:  null
----------------------结束---------------------------
{"collectContent":{"type":"string","format":null,"example":null,"xml":null,"position":null,"description":" 采集内容","title":null,"readOnly":null,"allowEmptyValue":null,"minLength":null,"maxLength":null,"pattern":null,"default":null,"enum":null},"collectEndTime":{"type":"string","format":"date-time","example":null,"xml":null,"position":null,"description":" 采集结束时间","title":null,"readOnly":null,"allowEmptyValue":null,"enum":null},"collectStartTime":{"type":"string","format":"date-time","example":null,"xml":null,"position":null,"description":" 采集开始时间","title":null,"readOnly":null,"allowEmptyValue":null,"enum":null},"collectionTaskId":{"type":"integer","format":"int64","example":null,"xml":null,"position":null,"description":" 信息采集任务id","title":null,"readOnly":null,"allowEmptyValue":null,"minimum":null,"maximum":null,"multipleOf":null,"exclusiveMinimum":null,"exclusiveMaximum":null,"default":null,"enum":null},"createBy":{"type":"string","format":null,"example":null,"xml":null,"position":null,"description":null,"title":null,"readOnly":null,"allowEmptyValue":null,"minLength":null,"maxLength":null,"pattern":null,"default":null,"enum":null},"createTime":{"type":"string","format":"date-time","example":null,"xml":null,"position":null,"description":null,"title":null,"readOnly":null,"allowEmptyValue":null,"enum":null},"dataRetrieval":{"type":"string","format":null,"example":null,"xml":null,"position":null,"description":" 资料调阅","title":null,"readOnly":null,"allowEmptyValue":null,"minLength":null,"maxLength":null,"pattern":null,"default":null,"enum":null},"id":{"type":"integer","format":"int64","example":null,"xml":null,"position":null,"description":null,"title":null,"readOnly":null,"allowEmptyValue":null,"minimum":null,"maximum":null,"multipleOf":null,"exclusiveMinimum":null,"exclusiveMaximum":null,"default":null,"enum":null},"informationCollectionId":{"type":"integer","format":"int64","example":null,"xml":null,"position":null,"description":" 信息采集id","title":null,"readOnly":null,"allowEmptyValue":null,"minimum":null,"maximum":null,"multipleOf":null,"exclusiveMinimum":null,"exclusiveMaximum":null,"default":null,"enum":null},"isArchived":{"type":"string","format":null,"example":null,"xml":null,"position":null,"description":null,"title":null,"readOnly":null,"allowEmptyValue":null,"minLength":null,"maxLength":null,"pattern":null,"default":null,"enum":null},"projectName":{"type":"string","format":null,"example":null,"xml":null,"position":null,"description":" 项目名称","title":null,"readOnly":null,"allowEmptyValue":null,"minLength":null,"maxLength":null,"pattern":null,"default":null,"enum":null},"relatedMaterials":{"type":"string","format":null,"example":null,"xml":null,"position":null,"description":" 相关材料","title":null,"readOnly":null,"allowEmptyValue":null,"minLength":null,"maxLength":null,"pattern":null,"default":null,"enum":null},"relatedMaterialsUrl":{"type":"string","format":null,"example":null,"xml":null,"position":null,"description":" 相关材料url","title":null,"readOnly":null,"allowEmptyValue":null,"minLength":null,"maxLength":null,"pattern":null,"default":null,"enum":null},"remark":{"type":"string","format":null,"example":null,"xml":null,"position":null,"description":" 备注","title":null,"readOnly":null,"allowEmptyValue":null,"minLength":null,"maxLength":null,"pattern":null,"default":null,"enum":null},"status":{"type":"string","format":null,"example":null,"xml":null,"position":null,"description":" 状态","title":null,"readOnly":null,"allowEmptyValue":null,"minLength":null,"maxLength":null,"pattern":null,"default":null,"enum":null},"type":{"type":"integer","format":"int32","example":null,"xml":null,"position":null,"description":" 提交类型 1:提交 2:追加提交","title":null,"readOnly":null,"allowEmptyValue":null,"minimum":null,"maximum":null,"multipleOf":null,"exclusiveMinimum":null,"exclusiveMaximum":null,"default":null,"enum":null},"updateBy":{"type":"string","format":null,"example":null,"xml":null,"position":null,"description":null,"title":null,"readOnly":null,"allowEmptyValue":null,"minLength":null,"maxLength":null,"pattern":null,"default":null,"enum":null},"updateTime":{"type":"string","format":"date-time","example":null,"xml":null,"position":null,"description":null,"title":null,"readOnly":null,"allowEmptyValue":null,"enum":null}}
name: collectionTaskCommitRecordEntity   in: body || required: true || description: collectionTaskCommitRecordEntity
----------------------请求参数结束---------------------------

4、导出Excel样例

相关推荐
hqyjzsb44 分钟前
2026年AI证书选择攻略:当“平台绑定”与“能力通用”冲突,如何破局?
大数据·c语言·人工智能·信息可视化·职场和发展·excel·学习方法
牛奔1 小时前
Linux 的日志分析命令
linux·运维·服务器·python·excel
不吃葱的胖虎4 小时前
根据Excel模板,指定单元格坐标填充数据
java·excel
罗政4 小时前
【Excel批处理】一键批量AI提取身份证信息到excel表格,数据安全,支持断网使用
人工智能·excel
晨晨渝奇4 小时前
pandas 中将两个 DataFrame 分别导出到同一个 Excel 同一个工作表(sheet1)的 A1 单元格和 D1 单元格
excel·pandas
木辰風4 小时前
EasyExcel根据动态字段,进行导出excel文件
java·前端·excel
辣机小司4 小时前
【踩坑记录:EasyExcel 生产级实战:策略模式重构与防御性导入导出校验指南(实用工具类分享)】
java·spring boot·后端·重构·excel·策略模式·easyexcel
傻啦嘿哟1 天前
Python实现Excel数据自动化处理:从繁琐操作到智能流程的蜕变
python·自动化·excel
hqyjzsb2 天前
从爱好到专业:AI初学者如何跨越CAIE认证的理想与现实鸿沟
大数据·c语言·人工智能·信息可视化·职场和发展·excel·业界资讯
vfvfb2 天前
excel对比找不同人名 双xlsx双列对比
excel·excel多列对比