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

转自:https://blog.csdn.net/qq_33500630/article/details/79242427

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

c068c05d027a96aa4c16fe10ef8bf5a5.png

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

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

版权声明:本文为weixin_34397537原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。