读取@RequestMapping生成路径列表

引言

在试用springmvc时,使用@RequestMapping进行路径映射。特别是移动端,需要对映射路径进行打印,生成表格,以便其他平台进行模板调用。于是我们来讨论一下

基本配置

对该项目需要整理的路径的基本读取配置。
这里选用项目路径,路径包,输出的位置,是否直接覆盖文件这四个配置项。

    //项目路径
    private static String PROJECT_PATH = "F:/eclipse/workspace/created/src/";
    //扫描包
    private static String SACNNER_PACKAGE = "com/create/api/";
    //输出文件的映射路径的位置
    private static String WRITE_TXT_PATH = "F:/mapper.txt";
    //是否直接覆盖文件
    private static boolean RE_WRITE = true;

主体代码

以下为主题代码内容

    /**
     * 写出文档
     */
    private void writerMapper(){
        File write_file = new File(WRITE_TXT_PATH);
        if(write_file.exists()&&!RE_WRITE){
            System.out.println("文件已存在  "+write_file.getAbsolutePath());
            return;
        }
        
        if(RE_WRITE){
            System.out.println("文件覆盖写入...");
        }
        
        String[] filepathes = getFilePathes(PROJECT_PATH+SACNNER_PACKAGE);
        Writer w= null;
        try {
            w = new FileWriter(write_file);
            for (int i = 0; i < filepathes.length; i++) {
                File f = new File(PROJECT_PATH+SACNNER_PACKAGE+filepathes[i]);
                try {
                    w.write("\r\n");
                    w.write("[======="+filepathes[i]+"========]\r\n");
                    w.write(readMapper(f));
                    w.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            w.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    
        System.out.println("文件写入完成  "+write_file.getAbsolutePath());
    }
    /**
     * 获取目标路径下所有的文件名称
     * @param parentPath
     * @return
     */
    private String[] getFilePathes(String parentPath){
        File f = new File(parentPath);
        String[] fiel = f.list();
        return fiel;
    }
    /**
     * 读取文件下的@RequestMapper注解,将整理好的路径内容返回
     * @param parentPath
     */
    private String readMapper(File f) throws IOException{
        BufferedReader br = new BufferedReader(new FileReader(f));
        StringBuffer sb = new StringBuffer();
        int i = 0;
        String classpath = "";
        while (br.ready()){
            String mapper = br.readLine();
            mapper = trim(mapper);
            if(mapper.startsWith("@RequestMapping")){
                String m = mapper.substring("@RequestMapping(".length(),mapper.length()-1);
                m = m.replace("value=", "");
                m = m.replace("method=RequestMethod.", "");
                m = m.replace("\"", "");
                if(i==0){
                    classpath = m;
                    i++;
                    continue;
                }else{
                    sb.append(i+". ");
                }
                sb.append(classpath);
                sb.append(m);
                if(i>0)
                    if(!(m.endsWith("GET")||m.endsWith("POST")))
                        sb.append(",GET");
                String nextLine = br.readLine();
                nextLine = trim(nextLine);
                String ending = "    页面跳转";
                if(nextLine.startsWith("@ResponseBody")){
                    ending="    获取数据 ";
                }
                sb.append(ending);
                sb.append("  [ ]");
                sb.append("\r\n");
                i++;
            } 
        }
        br.close();
        return sb.toString();
    }

去掉空白字符

在操作时需要对空白字符全部去除,空白字符包括
\n 回车(\u000a) 
\t 水平制表符(\u0009)
\s 空格(\u0008) 
\r 换行(\u000d)

具体操作

    /**
     * 去掉空白字符
     * @param str
     * @return
     */
    private String trim(String str){
        str = str.replace(" ", "")
        .replace("\t", "")
        .trim();
        return str;
    }

至此已经完成内容

内容扩展

在内容上面需要扩展的部分

写的时候发现,导出的路径方式需要提供内容描述,但是本文中只提供了[] 符号,用于填写路径描述,所以该部分不够完善。

因此,在完成这部分时,需要添加注释注解 @title 或者此外的任意便于识别的注释注解,在读取时,就将标题内容读取出来,在读取到该部分的时候将标题插入。

去掉的字符应该包括 [value=] [param=]两组  

更多扩展需要自己拓展。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值