一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 詳解獲取Spring MVC中所有RequestMapping以及對應方法和參數

詳解獲取Spring MVC中所有RequestMapping以及對應方法和參數

2020-08-29 11:40Y橡樹Y Java教程

本篇文章主要介紹了詳解獲取Spring MVC中所有RequestMapping以及對應方法和參數,具有一定的參考價值,感興趣的小伙伴們可以參考一下。

Spring MVC中想要對每一個URL進行權限控制,不想手工整理這樣會有遺漏,所以就動手寫程序了。代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
 * @return
 * @author Elwin ZHANG
 * 創建時間:2017年3月8日 上午11:48:22
 * 功能:返回系統中的所有控制器映射路徑,以及對應的方法
 */
@RequestMapping(value = "/maps", produces = "application/json; charset=utf-8")
@ResponseBody
public Object getMapPaths(){
  String result="";
   RequestMappingHandlerMapping rmhp = springHelper.getObject(RequestMappingHandlerMapping.class);
    Map<RequestMappingInfo, HandlerMethod> map = rmhp.getHandlerMethods();
    for(RequestMappingInfo info : map.keySet()){
      result +=info.getPatternsCondition().toString().replace("[", "").replace("]", "")+ "\t"   ;
      HandlerMethod hm=map.get(info);
      result +=hm.getBeanType().getName()+ "\t"   ;
      result +=getMethodParams(hm.getBeanType().getName(),hm.getMethod().getName())+ "\t";
      result +=info.getProducesCondition().toString().replace("[", "").replace("]", "")+ "\t"   ;
      result += "\r\n";
    }
  return result;
}

getMethodParams是專門用于獲取方法中參數名稱的函數,因為用Java自身的反射功能是獲取不到的,浪費我不少時間,后來網上看到JBOSS的JAVAssist類可以。其實這個JAVAssist類庫也被封裝在Mybatis中,如果系統使用了Mybatis,則直接引入可以使用了。

?
1
2
import org.apache.ibatis.javassist.*;
import org.apache.ibatis.javassist.bytecode.*;

getMethodParams 的實現如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
   * @param className 類名
   * @param methodName 方法名
   * @return 該方法的聲明部分
   * @author Elwin ZHANG
   * 創建時間:2017年3月8日 上午11:47:16
   * 功能:返回一個方法的聲明部分,包括參數類型和參數名
   */
  private String getMethodParams(String className,String methodName){
    String result="";
    try{
      ClassPool pool=ClassPool.getDefault();
       ClassClassPath classPath = new ClassClassPath(this.getClass());
       pool.insertClassPath(classPath);
      CtMethod cm =pool.getMethod(className, methodName);
     // 使用javaassist的反射方法獲取方法的參數名
      MethodInfo methodInfo = cm.getMethodInfo();
      CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
      LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);
      result=cm.getName() + "(";
      if (attr == null) {
        return result + ")";
      }
      CtClass[] pTypes=cm.getParameterTypes();
      String[] paramNames = new String[pTypes.length];
      int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;
      for (int i = 0; i < paramNames.length; i++) {
        if(!pTypes[i].getSimpleName().startsWith("HttpServletRe")){
          result += pTypes[i].getSimpleName();
          paramNames[i] = attr.variableName(i + pos);
          result += " " + paramNames[i]+",";
        }
      }
      if(result.endsWith(",")){
        result=result.substring(0, result.length()-1);
      }
      result+=")";
    }catch(Exception e){
      e.printStackTrace();
    }
    return result;
  }

這樣就可以獲得每個URL路徑與期對應的方法聲明了。

另外SpringHelper是自己封裝的Spring工具類,可以用來直接獲取Spring管理的Bean,代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
 
/**
 * @author Elwin ZHANG
 * 創建時間:2016年4月14日 上午9:12:13
 * 功能:Spring 工具類,用于獲取Spring管理的Bean
 */
@Component
public class SpringHelper implements ApplicationContextAware {
  // 日志輸出類
  private static Logger logger = Logger.getLogger(SpringHelper.class);
 
  // 當前的Spring上下文
  private static ApplicationContext applicationContext;
 
  @Override
  public void setApplicationContext(ApplicationContext arg0)
      throws BeansException {
    applicationContext = arg0;
  }
 
  /**
   * @param beanName bean Id
   * @return 如果獲取失敗,則返回Null
   * @author Elwin ZHANG
   * 創建時間:2016年4月14日 上午9:52:55
   * 功能:通過BeanId獲取Spring管理的對象
   */
  public Object getObject(String beanName) {
    Object object = null;
    try {
      object = applicationContext.getBean(beanName);
    } catch (Exception e) {
      logger.error(e);
    }
    return object;
  }
 
 
  /**
   * @return
   * @author Elwin ZHANG
   * 創建時間:2017年3月7日 下午3:44:38
   * 功能:獲取Spring的ApplicationContext
   */
  public ApplicationContext getContext() {
    return applicationContext;
  }
 
  /**
   * @param clazz 要獲取的Bean類
   * @return 如果獲取失敗,則返回Null
   * @author Elwin ZHANG
   * 創建時間:2016年4月14日 上午10:05:27
   * 功能:通過類獲取Spring管理的對象
   */
  public <T> T getObject(Class<T> clazz) {
    try {
      return applicationContext.getBean(clazz);
    } catch (Exception e) {
      logger.error(e);
    }
    return null;
  }
 
/**
 * @param code 配置文件中消息提示的代碼
 * @param locale 當前的語言環境
 * @return 當前語言對應的消息內容
 * @author Elwin ZHANG
 * 創建時間:2016年4月14日 上午10:34:25
 * 功能:獲取當前語言對應的消息內容
 */
  public String getMessage(String code,Locale locale){
    String message;
    try{
      message=applicationContext.getMessage(code, null, locale);
    }catch(Exception e){
      logger.error(e);
      message="";
    }
    return message;
  }
 
  /**
   *
   * @param code 配置文件中消息提示的代碼
   * @param request 當前的HTTP請求
   * @return 當前語言對應的消息內容
   * @author Elwin ZHANG
   * 創建時間:2016年4月14日 下午3:03:37
   * 功能:獲取當前語言對應的消息內容
   */
  public String getMessage(String code,HttpServletRequest request){
    String message;
    try{
      message=applicationContext.getMessage(code, null, getCurrentLocale(request));
    }catch(Exception e){
      logger.error(e);
      message="zh_CN";
    }
    return message;
  }
 
  /**
   * @param request 當前的HTTP請求
   * @return 當前用戶Cookie中的語言
   * @author Elwin ZHANG
   * 創建時間:2016年4月14日 下午2:59:21
   * 功能:當前用戶保存Cookie中的默認語言
   */
  public Locale getCurrentLocale(HttpServletRequest request){
    return resolver.resolveLocale(request);
  }
 
  //Cookie本地語言解析器,Spring提供
  @Autowired
  CookieLocaleResolver resolver;
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://www.jianshu.com/p/bb9e0c402341#

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 99精品视频在线观看免费播放 | 美女免费观看一区二区三区 | 亚洲欧美一区二区三区不卡 | 91人人| 国产精品第一区揄拍 | 免费观看无人区完整版 | 美女班主任下面好爽好湿好紧 | 国内精品一区二区在线观看 | 色老女人| 欧美日韩在线一区二区三区 | 精品人人做人人爽久久久 | 91啦中文在线观看 | 国产一级毛片潘金莲的奶头 | 色中文字幕 | 95在线观看精品视频 | 国产欧美日韩在线观看精品 | 攻插受 | 色播艾小青国产专区在线播放 | 欧美 变态 另类 人妖班 | 99免费视频 | 午夜欧美精品 | 99福利影院 | 国产一区二区不卡 | 99ri在线精品视频在线播放 | 久久这里只精品热在线18 | 亚洲精品国产一区二区第一页 | 欧美视频一区二区三区四区 | 国内精品哆啪啪 | 亚洲 日韩 国产 制服 在线 | 欧美一级片免费在线观看 | 亚洲剧情在线观看 | gogo人体模特啪啪季玥图片 | 欧美穿高跟鞋做爰 | 亚洲伦理一区 | 999热这里只有精品 999久久久免费精品国产牛牛 | 视频在线观看高清免费 | 成年性生交大片免费看 | 999久久久免费精品国产牛牛 | 青青草在视线频久久 | 喜欢老头吃我奶躁我的动图 | 欧美se图|