首页 > 软件开发 > Java > 脚本分享-打印Spring Mvc容器中的所有接口

脚本分享-打印Spring Mvc容器中的所有接口

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * 描述:打印spring mvc所有控制层接口
 *
 * @author Rabbiter
 * @version 1.0
 * @since 2020/11/10
 */
@Component
public class ListApi {
    @Resource
    ConfigurableApplicationContext run;

    /**
     * 打印spring mvc中的接口
     *
     * @return return value
     */
    @Bean
    public Object printApi() {
        System.out.println("===== 开始打印 spring mvc 接口");
        try {
            //获取controller注解的bean名称
            String[] beanNamesForAnnotation = run.getBeanNamesForAnnotation(RestController.class);
            //获取类对象
            for (String str : beanNamesForAnnotation) {
                Object bean = run.getBean(str);
                Class<?> controllerClazz = bean.getClass();
                //获取requestmapping注解的类
                RequestMapping requestMappingOnClazz = controllerClazz.getAnnotation(RequestMapping.class);
                String urlPath = null;
                String[] value = (requestMappingOnClazz == null || requestMappingOnClazz.value().length == 0
                        ? new String[]{""} : requestMappingOnClazz.value());
                //获取类的url路径
                urlPath = value[0];
                if (!"".equals(urlPath) && !"/".equals(urlPath.substring(0, 1))) {
                    //补充斜杠
                    urlPath = "/" + urlPath;
                }
                Method[] controllerMethods = controllerClazz.getDeclaredMethods();
                for (Method method : controllerMethods) {
                    RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);
                    GetMapping getMapping = method.getAnnotation(GetMapping.class);
                    PostMapping postMapping = method.getAnnotation(PostMapping.class);
                    if (getMapping != null) {
                        //GET方法
                        printGetMsg(urlPath, getMapping, controllerClazz, method);
                    } else if (postMapping != null) {
                        //POST方法
                        printPostMsg(urlPath, postMapping, controllerClazz, method);
                    } else if (requestMapping != null) {
                        //未指定方法
                        printUnknownMsg(urlPath, requestMapping, controllerClazz, method);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("===== 打印接口过程中出现异常,结束打印");
        } finally {
            System.out.println("===== spring mvc 接口打印结束");
        }
        return new Object();
    }

    /**
     * 处理get接口
     *
     * @param urlPath         公共路径前缀
     * @param getMapping      注解
     * @param controllerClazz 接口所在controller的类对象
     * @param method          接口所在方法对象
     */
    private void printGetMsg(String urlPath, GetMapping getMapping, Class<?> controllerClazz, Method method) {
        String[] urls = getMapping.value();
        for (String url : urls) {
            if (url.length() != 0 && !"/".equals(url.substring(0, 1))) {
                //补充斜杠
                url = "/" + url;
            }
            System.out.println(urlPath + url + " --- GET");
        }
    }

    /**
     * 处理post接口
     *
     * @param urlPath         公共路径前缀
     * @param postMapping     注解
     * @param controllerClazz 接口所在controller的类对象
     * @param method          接口所在方法对象
     */
    private void printPostMsg(String urlPath, PostMapping postMapping, Class<?> controllerClazz, Method method) {
        String[] urls = postMapping.value();
        for (String url : urls) {
            if (url.length() != 0 && !"/".equals(url.substring(0, 1))) {
                //补充斜杠
                url = "/" + url;
            }
            System.out.println(urlPath + url + " --- POST");
        }
    }

    /**
     * 处理再requestMapping中指定的类型接口
     *
     * @param urlPath         公共路径前缀
     * @param requestMapping  注解
     * @param controllerClazz 接口所在controller的类对象
     * @param method          接口所在方法对象
     */
    private void printUnknownMsg(String urlPath, RequestMapping requestMapping, Class<?> controllerClazz, Method method) {
        String[] urls = requestMapping.value();
        for (String url : urls) {
            if (url.length() != 0 && !"/".equals(url.substring(0, 1))) {
                //补充斜杠
                url = "/" + url;
            }
            System.out.println(urlPath + url + " --- " + Arrays.toString(requestMapping.method()));
        }
    }
}

 

猜你喜欢

Java常用API

Java常用API

Java工具类整理

Java工具类整理

Shiro框架的基本使用及问题解决

Shiro框架的基本使用及问题解决

简单实现MyBatis

简单实现MyBatis

关于 CSharp 中调用非托管代码的方法

关于 CSharp 中调用非托管代码的方法

0 条评论

img 登陆后才能评论哦~