天天看點

AOP攔截Controller擷取@PathVariable注解傳入的參數

前言:

最近項目中需要對controller傳入的應用辨別(appMarkId)進行校驗,[email protected]傳入到url模版中的,這裡用到了aop統一攔截處理,但是在攔截的過程中發現request擷取不到該參數,随後進行了研究。

示例代碼:

AOP攔截Controller擷取@PathVariable注解傳入的參數

處理:

Map map = (Map) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);

           

可以用這個方法擷取到所有uri模版中傳入的參數。

AOP攔截Controller擷取@PathVariable注解傳入的參數

擴充: aop中擷取request和response

ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
HttpServletRequest request = servletRequestAttributes.getRequest();
HttpServletResponse response = servletRequestAttributes.getResponse();

           

或者直接通過@Autowried注入。