一个根据URI定位到spring mvc映射代码工具类

@Controller
@RequestMapping("/admin/util")
public class SystemController {
    private static final Logger log = LoggerFactory.getLogger(SystemController .class);
    
    @RequestMapping(value = "/findUriMapMethod.do")
    @ResponseBody
    public String findUriMapMethod(HttpServletRequest request, HttpServletResponse response) {
        final Env env = EnvUtils.getEnv();
        final String uri = env.param("uri", request.getRequestURI());
        return getHandler(request, uri, "GET");
    }

    private String getHandler(HttpServletRequest request, final String uri, String method) {
        final Env env = EnvUtils.getEnv();
        final String fMethod = method;
        String[] beanNames = env.getApplicationContext().getBeanNamesForType(RequestMappingHandlerMapping.class);
        log.info("RequestMappingHandlerMapping: {}", Arrays.toString(beanNames));
        HttpServletRequestWrapper httpServletRequestWrapper = new HttpServletRequestWrapper(request) {
            @Override
            public String getRequestURI() {
                /*String paramUri = super.getParameter("uri");
                if(paramUri != null && !"".equals(paramUri.trim())) {
                    return paramUri;
                }*/
                return uri;
            }
            
            @Override
            public StringBuffer getRequestURL() {
                return new StringBuffer(super.getRequestURL().toString()
                .replace(super.getRequestURI(), uri));
            }
            
            @Override
            public String getServletPath() {
                return super.getServletPath().replace(super.getRequestURI(), uri);
            }
            
            @Override
            public String getMethod() {
                if(fMethod == null || "".equals(fMethod)) {
                    return super.getMethod();
                }
                return fMethod;
            }
        };
        StringBuilder uriMapMethod = new StringBuilder();
        uriMapMethod.append(httpServletRequestWrapper.getRequestURI()).append(": [");
        if(beanNames != null) {
            for(String beanName : beanNames) {
                log.info("beanName: {} ", beanName);
                RequestMappingHandlerMapping mapping = env.getBean(beanName, 
                RequestMappingHandlerMapping.class);
                try {
                    HandlerExecutionChain chain = mapping.getHandler(httpServletRequestWrapper);
                    if(chain != null) {
                        Object handler = chain.getHandler();
                        System.out.println(handler);
                        if(handler instanceof HandlerMethod) {
                            HandlerMethod hm = (HandlerMethod)handler;
                            log.info("{}:{}", hm.getBeanType().getName(),hm);
                            uriMapMethod.append(hm);
                        } else if(handler instanceof org.springframework.web.servlet.mvc.Controller) {
                            org.springframework.web.servlet.mvc.Controller hm = (org.springframework.web.servlet.mvc.Controller)handler;
                            Class<? extends org.springframework.web.servlet.mvc.Controller> hmClass = hm.getClass();
                            log.info("{}:{}", hmClass.getName(), hmClass.getDeclaredMethod("handleRequest", 
                            HttpServletRequest.class, HttpServletResponse.class));
                            uriMapMethod.append(hmClass.getDeclaredMethod("handleRequest", 
                            HttpServletRequest.class, HttpServletResponse.class));
                        } else {
                            uriMapMethod.append(handler.getClass().getName());
                        }
                        break;
                    }
                } catch (HttpRequestMethodNotSupportedException e) {
                     return getHandler(httpServletRequestWrapper, uri, "POST");
                } catch (Exception e) {
                    log.error("get uri mapping error.", e); 
                }
                /*Map<RequestMappingInfo, HandlerMethod> mapMethods =  mapping.getHandlerMethods();
                if(mapMethods != null) {
                    Iterator<Entry<RequestMappingInfo, HandlerMethod>> iter = mapMethods.entrySet().iterator();
                    while (iter.hasNext()) {
                        Entry<RequestMappingInfo, HandlerMethod> entry = iter.next();
                        RequestMappingInfo key = entry.getKey();
                        HandlerMethod hm = (HandlerMethod)entry.getValue();
                        Method method = hm.getMethod();
                        log.info("{} : {}->{}", key.getPatternsCondition(), key, hm);
                    }
                }*/
            }
        }
        return uriMapMethod.append("]").toString();
    }
}

可以方便是根据URI定位到spring mvc的controller代码,

时间: 2024-07-31 03:09:27

一个根据URI定位到spring mvc映射代码工具类的相关文章

自定义MVC框架之工具类-图像处理类

截止目前已经改造了4个类: ubuntu:通过封装验证码类库一步步安装php的gd扩展 自定义MVC框架之工具类-分页类的封装 自定义MVC框架之工具类-文件上传类 图像处理类: 1,图片加水印处理( 支持任意位置与九宫格位置 ) 2,图片缩放处理( 等比缩放,不变形裁剪 ) 1 <?php 2 3 class Image{ 4 //文件路径 5 protected $path; 6 //是否允许随机文件名称 7 protected $enableRandName; 8 //文件类型 9 pro

自定义MVC框架之工具类-模型类

截止目前已经改造了5个类: ubuntu:通过封装验证码类库一步步安装php的gd扩展 自定义MVC框架之工具类-分页类的封装 自定义MVC框架之工具类-文件上传类 自定义MVC框架之工具类-图像处理类 这个模型类支持以下功能: >连贯操作,js叫链式操作,连贯操作的函数可以打乱顺序,最后一个函数必须是执行语句的那个函数,如select, delete, update, add等 如 $db->table( 'user' )->where( 'id=1' )->select() 等

获取Spring容器Bean对象工具类

在开发中,总是能碰到用注解注入不了Spring容器里面bean对象的问题.为了解决这个问题,我们需要一个工具类来直接获取Spring容器中的bean.因此就写了这个工具类,在此记录一下,方便后续查阅.废话不多说,直接上代码. 一.代码 package com.zxy.demo.spring; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext;

Spring MVC常用的注解类

一.注解类配置 要使用springmvc的注解类,需要在springmvc.xml配置文件中用context:component-scan/扫描: ? 二.五大重要的注解类 1.RequestMapping注解 RequestMapping注解类的使用方法 在Controller控制器类的类定义和方法定义处都可以标注@RequestMapping注解 DispatcherServlet截获请求后,就可以通过控制器上的@RequestMapping提供的映射信息确定请求所对应的处理方法 packa

Spring MVC自定义统一异常处理类,并且在控制台中输出错误日志

在使用SimpleMappingExceptionResolver实现统一异常处理后(参考Spring MVC的异常统一处理方法), 发现出现异常时,log4j无法在控制台输出错误日志.因此需要自定义一个继承至SimpleMappingExceptionResolver的 RrtongMappingExceptionResolver类,在RrtongMappingExceptionResolver中通过 log.error(ex.getMessage())的方式输出日志到控制台上.以下是具体的配

一个加速产生 Spring JDBC RowMapper 的工具类

在使用Spring JdbcTemplate 时,编写 RowMapper 实在是一件累人的工作.于是我写了一个根据实体类产生 RowMapper 的工厂类 RowMapperFactory,来避免直接编写 RowMapper.RowMapperFactory 暂不支持枚举. 下面就是 RowMapperFactory: import org.springframework.jdbc.core.RowMapper; import java.lang.reflect.Method; import

Spring MVC异常处理代码完整实例

Spring MVC异常处理流程: 提供构造方法传值: 配置异常处理器的bean 原文地址:https://www.cnblogs.com/niwotaxuexiba/p/11218171.html

spring mvc 项目代码

说明:JAVA SpringMVC+mybatis(oracle 和 mysql) HTML5 全新高大尚后台框架 1.支持APP手机应用(android和ios)接口调用(json接口可与其它程序对接) 2.全新高大尚全HTML5+css3.0开发界面,美观漂亮时尚.前沿 3.有ORACLE 和MYSQL 版本各一个 4.框架搭建完善,在此基础上做过很多项目,身经百战,支持大并发,程序运行稳定 5.基础功能已经完善 组织管理:类似角色管理,分角色组和成员,有组权限和成员权限.菜单权限,独立分配

Spring 常用的一些工具类

学习Java的人,或者开发很多项目,都需要使用到Spring 这个框架,这个框架对于java程序员来说.学好spring 就不怕找不到工作.我们时常会写一些工具类,但是有些时候 我们不清楚,我们些的工具类,是否稳定,可靠.对于有看spring 源码习惯的人,其实,spring框架本身自带了很多工具类,其实,我有一个想法,就是想把一些常用的方法,从spring 整理整理出来,然后编译成jar包,因为有些时候,项目并不需要引用所有jar包进入的.这边整理了一些spring 常用的类,共大家参照: s