Spring controller的url扫描

spring rest项目url规范

用到很多的url,有的不符合规范,需要统计所有url查看哪些不合理,哪些需要改进的。就写了一个小程序来读取Controller的注解来分析rest所用到的url。添加了递归调用的方式,更方便读取。

分析后的结果

package com.lanaya.web.util;

/**
 * @author dongbin.yu
 * @version 1.0.0
 * @since 2015/12/21
 */

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.io.File;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * @author dongbin.yu
 * @version 1.0.0
 * @since 2015/10/21
 */
public class RequestMappingUtil {

        private static String controllerUrl = "com.lanaya.web.controller";

        public static void main(String[] args){
            URL resource = RequestMappingUtil.class.getClass().getResource("/" + controllerUrl.replace('.','/'));

            ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

            File directory = new File(resource.getPath());
            if(directory.isDirectory()){
                File[] files = directory.listFiles();
                for (File file : files) {
                    System.out.println(exportUrl(file));
                }

            }

        }

        public static List<String> exportUrl(File file){

            List<String> urlList = new ArrayList<>();

            try {
                if (file.isDirectory()) {
                    File[] files = file.listFiles();
                    for (File file1 : files) {
                        urlList.addAll(exportUrl(file1));
                    }
                } else {

                    String fileName = file.getName();
                    Class<?> clazz = Class.forName(controllerUrl + "." + fileName.substring(0, fileName.indexOf(".")));
                    RequestMapping requestMapping = clazz.getAnnotation(RequestMapping.class);

                    String topUrl = "";
                    if (requestMapping != null) {
                        topUrl = requestMapping.value()[0];
                    }

                    Method[] methods = clazz.getMethods();
                    for (Method method : methods) {
                        RequestMapping annotation = method.getAnnotation(RequestMapping.class);
                        if (annotation != null) {
                            String methodUrl = annotation.value()[0];

                            if (methodUrl.startsWith("/")) {
                                urlList.add(topUrl + methodUrl + ":" + (annotation.method().length == 0 ? RequestMethod.GET : annotation.method()[0]));
                            } else {
                                urlList.add(topUrl + File.separator + methodUrl + ":" + (annotation.method().length == 0 ? RequestMethod.GET : annotation.method()[0]));
                            }
                        }
                    }
                }

            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

            return urlList;
        }
}



通过读取RequestMapping注解和method注解,读取的时候要注意是否在Class头上有前缀。还有是否有层级嵌套。

attributionReport/getAttributionPathList:POST

调用后发现RequestMapping的value和method都是数组,可以接受多个参数的。





转载于:https://my.oschina.net/zooy/blog/522302

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值