前言:
最近項目中需要對controller傳入的應用辨別(appMarkId)進行校驗,[email protected]傳入到url模版中的,這裡用到了aop統一攔截處理,但是在攔截的過程中發現request擷取不到該參數,随後進行了研究。
示例代碼:
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZuBnL5gjN3EDNyQTMwITOwkTMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
處理:
Map map = (Map) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
可以用這個方法擷取到所有uri模版中傳入的參數。
擴充: aop中擷取request和response
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
HttpServletRequest request = servletRequestAttributes.getRequest();
HttpServletResponse response = servletRequestAttributes.getResponse();
或者直接通過@Autowried注入。