一个根据URI定位到spring mvc映射代码工具类详解编程语言
2023-06-13 09:20:23 时间
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();
}
来自:http://my.oschina.net/u/565351/blog/372300
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/8021.html
cjava相关文章
- Spring学习笔记(三十一)——SpringBoot JPA优雅高效的工具:QueryHelp
- 【面试篇】Spring的那些面试题(上)
- Spring中@Autowired和@Resource的区别
- Quartz与Spring集成—— SchedulerFactoryBean的初始化分析「建议收藏」
- Spring Cloud 学习笔记(2 / 3)
- 详解Java中的Spring框架
- 全文检索工具:第一章:Spring-data-elasticSearch搜索
- 玩转 SpringBoot 监控统计(SQL监控、慢SQL记录、Spring监控、去广告)
- Spring Boot 实现日志链路追踪,无需引入组件,让日志定位更方便!
- Spring Boot+Keycloak从零到壹
- Spring循环依赖-spring源码详解(四)
- Spring Controller
- 终于把 Spring Boot 3.0 写成书了!
- 第一个Spring MVC程序
- spring的AOP(四)—-Spring AOP 编程(Advisor)详解编程语言
- Spring Boot实现热部署详解编程语言
- Spring Boot读取配置的 5 种方式详解编程语言
- Spring Boot(二十):使用spring-boot-admin对spring-boot服务进行监控详解编程语言
- Spring Boot(十四):spring boot整合shiro-登录认证和权限管理详解编程语言
- Spring Boot(十三):spring boot小技巧详解编程语言
- Spring Boot(五):spring data jpa的使用详解编程语言
- Spring整合Redis简单实现高效缓存(spring集成redis)
- Spring Boot 2.x :通过 spring-boot-starter-hbase 集成 HBase