国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Swagger轉(zhuǎn)換成Excel文件

這篇具有很好參考價(jià)值的文章主要介紹了Swagger轉(zhuǎn)換成Excel文件。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

1、添加swagger解析依賴包:

        <dependency>
            <groupId>io.swagger.parser.v3</groupId>
            <artifactId>swagger-parser</artifactId>
            <version>2.1.12</version>
        </dependency>

2、示例代碼:

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轉(zhuǎn)換成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");

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

        // 創(chuàng)建標(biāo)題行
        Row titleRow = sheet.createRow(0);
        Cell titleCell1 = titleRow.createCell(0);
        titleCell1.setCellValue("Path");
        Cell titleCell2 = titleRow.createCell(1);
        titleCell2.setCellValue("Description");
        // ... 添加其他需要的列標(biāo)題
        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";
        // 遍歷每個(gè)路徑
        for (String path : swagger.getPaths().keySet()) {
            // 遍歷每個(gè)操作
            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);
                //獲取請(qǐng)求參數(shù)
                List<Parameter> parameters = entry.getValue().getParameters();

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

                    //請(qǐng)求參數(shù)
                    System.out.println("----------------------請(qǐng)求參數(shù)開始---------------------------");
                    //請(qǐng)求參數(shù)
                    String name = param.getName();
                    String in = param.getIn();
                    //判斷請(qǐng)求參數(shù)方式
                    if("body".equals(in)){
                        System.out.println(name + "  : 請(qǐng)求參數(shù)是body------------------------------");
                        String capitalized = StrUtil.capitalizeFirstLetter(name); // 首字母大寫
                        System.out.println("首字母大寫轉(zhuǎn)換:  " + capitalized);

                        String replaceStr = capitalized.replace("Entity", "");
                        System.out.println("去掉Entity字符轉(zhuǎn)換:  " + replaceStr);

                        //獲取實(shí)體列表
                        Map<String, Model> definitions = swagger.getDefinitions();
                        for (String key : definitions.keySet()) {
                            Model value = definitions.get(key);
                            //判斷實(shí)體名稱是否匹配如果匹配,打印字段信息
                            if(replaceStr.equals(value.getTitle())){
                                // 處理key和value
                                System.out.println("----------------------開始---------------------------");
                                System.out.println("實(shí)體:   " + value.getTitle());
                                Map<String, Property> properties = value.getProperties();
                                //遍歷實(shí)體字段
                                properties.forEach((key1, value1) -> {
                                    // 字段值和字段描述:例如:projectName: 項(xiàng)目名稱
                                    System.out.println("字段:" + key1 + ":" + "  字段描述:  " + value1.getDescription());
                                });
                                System.out.println("----------------------結(jié)束---------------------------");

                                // 創(chuàng)建ObjectMapper實(shí)例
                                ObjectMapper mapper = new ObjectMapper();

                                // 將Map轉(zhuǎn)換為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("----------------------請(qǐng)求參數(shù)結(jié)束---------------------------");

                });

                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++;
            }
        }

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

}

3、打印信息樣例

----------------------請(qǐng)求參數(shù)開始---------------------------
collectionTaskCommitRecordEntity  : 請(qǐng)求參數(shù)是body------------------------------
首字母大寫轉(zhuǎn)換:  CollectionTaskCommitRecordEntity
去掉Entity字符轉(zhuǎn)換:  CollectionTaskCommitRecord
----------------------開始---------------------------
實(shí)體:   CollectionTaskCommitRecord
字段:collectContent:  字段描述:   采集內(nèi)容
字段:collectEndTime:  字段描述:   采集結(jié)束時(shí)間
字段:collectStartTime:  字段描述:   采集開始時(shí)間
字段:collectionTaskId:  字段描述:   信息采集任務(wù)id
字段:createBy:  字段描述:  null
字段:createTime:  字段描述:  null
字段:dataRetrieval:  字段描述:   資料調(diào)閱
字段:id:  字段描述:  null
字段:informationCollectionId:  字段描述:   信息采集id
字段:isArchived:  字段描述:  null
字段:projectName:  字段描述:   項(xiàng)目名稱
字段:relatedMaterials:  字段描述:   相關(guān)材料
字段:relatedMaterialsUrl:  字段描述:   相關(guān)材料url
字段:remark:  字段描述:   備注
字段:status:  字段描述:   狀態(tài)
字段:type:  字段描述:   提交類型 1:提交 2:追加提交
字段:updateBy:  字段描述:  null
字段:updateTime:  字段描述:  null
----------------------結(jié)束---------------------------
{"collectContent":{"type":"string","format":null,"example":null,"xml":null,"position":null,"description":" 采集內(nèi)容","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":" 采集結(jié)束時(shí)間","title":null,"readOnly":null,"allowEmptyValue":null,"enum":null},"collectStartTime":{"type":"string","format":"date-time","example":null,"xml":null,"position":null,"description":" 采集開始時(shí)間","title":null,"readOnly":null,"allowEmptyValue":null,"enum":null},"collectionTaskId":{"type":"integer","format":"int64","example":null,"xml":null,"position":null,"description":" 信息采集任務(wù)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":" 資料調(diào)閱","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":" 項(xiàng)目名稱","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":" 相關(guān)材料","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":" 相關(guān)材料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":" 狀態(tài)","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
----------------------請(qǐng)求參數(shù)結(jié)束---------------------------

4、導(dǎo)出Excel樣例
Swagger轉(zhuǎn)換成Excel文件,提升,學(xué)習(xí),excel,swagger文章來源地址http://www.zghlxwxcb.cn/news/detail-847882.html

到了這里,關(guān)于Swagger轉(zhuǎn)換成Excel文件的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包