spring mvc aop拦截controller层获取RequestBody反序列化后参数
最近,为了解耦,把一逻辑从interceptor抽出来,放在aop中处理,需要得到RequestBody。如下:
@Aspect @Configuration public class CheckAspect { private final Logger logger = LoggerFactory.getLogger(this.getClass()); // 定义切点Pointcut 自行写入对应的controller包路径 @Pointcut("execution(* com.hs.yido.controller.*.*(..))") public void excudeService() { } @Around("excudeService()") public Object doAround(ProceedingJoinPoint pjp) throws Throwable { RequestAttributes ra = RequestContextHolder.getRequestAttributes(); ServletRequestAttributes sra = (ServletRequestAttributes) ra; HttpServletRequest request = sra.getRequest(); String url = request.getRequestURL().toString(); String method = request.getMethod(); String uri = request.getRequestURI(); String queryString = request.getQueryString(); //这里可以获取到get请求的参数和其他信息 logger.info("请求开始, 各个参数, url: {}, method: {}, uri: {}, params: {}", url, method, uri, queryString); //重点 这里就是获取@RequestBody参数的关键 调试的情况下 可以看到o变量已经获取到了请求的参数 Object[] o = pjp.getArgs(); // result的值就是被拦截方法的返回值 Object result = pjp.proceed(); return result; } }