将swagger接口中的字段 转为java实体对象映射工具类

package com.product.xxxx.readfile;

import org.junit.Test;
import org.springframework.util.StringUtils;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class ReadApiData2Vo {


    /**
     * 将接口文档中的字段部分转为 java类字段格式
     * 先将 接口文档中的字段部分复制到一个文本中  注意编码utf-8
     *
     * 格式如
     * 如:
     * competencyName	能力名称		false
     * string
     * competencyType	能力服务类型(1-SaaS 2-镜像)		false
     * string
     * current			false
     * integer(int32)
     * isAdmin	是否是管理员 0-不是 1-是		false
     * string
     * size			false
     * integer(int32)
     * tenantId	租户id		false
     * integer(int32)
     * vendorCode	厂商编码		false
     * string
     *
     * 转换后
     * @ApiModelProperty("能力名称")
     * private String competencyName;
     *
     * @ApiModelProperty("能力服务类型(1-SaaS 2-镜像)")
     * private String competencyType;
     *
     * @ApiModelProperty("")
     * private Integer current;
     *
     * @ApiModelProperty("是否是管理员 0-不是 1-是")
     * private String isAdmin;
     *
     * @ApiModelProperty("")
     * private Integer size;
     *
     * @ApiModelProperty("租户id")
     * private Integer tenantId;
     *
     * @ApiModelProperty("厂商编码")
     * private String vendorCode;
     *
     */
    @Test
    public void readBean2ApiModel(){
        //创建字符输入流对象,负责读取文件
        try {
            File file =new File("D:\\api.txt");
            InputStreamReader rdCto = new InputStreamReader(new FileInputStream(file));

            BufferedReader bfreader = new BufferedReader(rdCto);


            File writeFile = new File("D:/", "text.txt");
            if(! writeFile.exists()){
                file.createNewFile();
            }

            FileOutputStream out = new FileOutputStream(writeFile);

            String newLine = System.getProperty("line.separator");
            String line = null;

            Boolean isDel = false;
            int lineNum = 1;
            String lastColumn = "";
            Map<String, String> descMap = new HashMap<>();
            while((line = bfreader.readLine()) != null){
//                System.out.println(line);
                if(! StringUtils.isEmpty(line)){
                    if(lineNum % 2 == 0){
                        System.out.println(line);
                        if(descMap.get(lastColumn) != null){
                            //    @ApiModelProperty("能力名称")
                            String apiStr = "@ApiModelProperty(\"" + descMap.get(lastColumn).trim() + "\")";
                            out.write(apiStr.getBytes());
                            out.write("\n".getBytes());
                            String columnType = "";
                            if(line.trim().equals("string")){
                                columnType = "String";
                            }else if(line.trim().contains("int")){
                                columnType = "Integer";
                            }
                            String columnProperty = "private " + columnType + " " + lastColumn + ";";
                            out.write(columnProperty.getBytes());
                            out.write("\n".getBytes());
                            out.write("\n".getBytes());
                        }
                    }else{
                        String[] columnArr = line.split("\t");
                        lastColumn = columnArr[0];
                        System.out.println(lastColumn);
                        descMap.put(lastColumn, columnArr[1]);
                    }
                    lineNum ++;
                }

            }

            out.close();
            bfreader.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{

        }
    }
}

猜你喜欢

转载自blog.csdn.net/wdd668/article/details/128416323