從Spring MVC中獲取Controller中的請求信息

   在我們平常的項目裏面經常需要進行權限配置,其中有一個就是先把項目裏面的請求的路徑存進去,之後在進行一系列的設置,但是每次都要從代碼裏面複製粘帖就很麻煩了。
   在Spring MVC中是可以很方便的獲取到請求的信息的。

在Spring MVC已經可用的情況下,直接注入RequestMappingHandlerMapping

//直接注入 RequestMappingHandlerMapping
@Resource
private RequestMappingHandlerMapping requestMappingHandlerMapping;
//獲取所有的請求信息
Map<RequestMappingInfo, HandlerMethod> requestMappingInfoMap = requestMappingHandlerMapping.getHandlerMethods();

RequestMappingInfo:就是@RequestMapping 對應的信息;
HandlerMethod:@RequestMapping對應方法的詳情,包括方法、類及參數。


下面是獲取幾個常用的信息:

 //請求路徑
PatternsRequestCondition patternsRequestCondition = requestMappingInfo.getPatternsCondition();
//請求方法(GET,POST等)
RequestMethodsRequestCondition requestMethodsRequestCondition = requestMappingInfo.getMethodsCondition();
//請求的媒體類型(application/json,text/html等)
ProducesRequestCondition producesCondition = requestMappingInfo.getProducesCondition();
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章