Spring controller的url扫描

为什么80%的码农都做不了架构师?>>>   hot3.png

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 exportUrl(File file){

            List 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

你可能感兴趣的:(Spring controller的url扫描)