springboot拦截器获取接口返回值_SpringMVC拦截器获取@Response的返回值

太过爱你忘了你带给我的痛 2023-01-12 10:23 375阅读 0赞

转自:https://blog.csdn.net/qq\_33500630/article/details/79242427

在项目开发中,有时候我们需求在拦截器中获取@responsebody的返回值,但是在我的知识体系中没有方法获取,(除非研究源码),但是在springMVC4版本以后,新加了一个@ControllerService注解。用此注解然后实现ResponseBodyAdvice接口可获取带有@responsebody的返回值,然后操作。

c068c05d027a96aa4c16fe10ef8bf5a5.png

然后在springmvc拦截器中afterCompletion的方法中进行获取。

  1. @Override
  2. public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
  3. // 将handler强转化为handlermethod
  4. HandlerMethod handlerMethod = (HandlerMethod) handler;
  5. // 从方法处理器中获取要调用的方法
  6. Method method = handlerMethod.getMethod();
  7. //获取返回结果
  8. Object result = request.ge

发表评论

表情:
评论列表 (有 0 条评论,375人围观)

还没有评论,来说两句吧...

相关阅读