2017-08-25 70 views
0

我有一個彈簧啓動應用程序。現在我需要在interceptor.I讀取請求和響應使用HttpServletRequestWrapperDispatcherServlet如何在攔截器中讀取httpServletResponse?

@Component("dispatcherServlet") 
public class FofDisPatcherServlet extends DispatcherServlet { 
    @Override 
    protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception { 
     MultiReadHttpServletRequest requestWrapper = null; 
     try { 
      requestWrapper = new MultiReadHttpServletRequest(request); 
      super.doDispatch(requestWrapper, response); 
     } catch (Exception e) { 
      super.doDispatch(request,response); 
     } 
    } 
} 

而在我的攔截,請求更換,我可以讀請求主體。但是當我想讀取響應主體時,它不起作用。當我替換CustomerDispatcherServlet中的響應時,我沒有得到任何響應。我試過ContentCachingResponseWrapper,但是我用「」得到了有效載荷。

這是一個古老的問題。我搜索了一些問題,但沒有找到合適的解決方案。

我知道我可以用AOP來解決問題,但是我想知道在攔截器中怎麼做?


這裏是我的代碼攔截

public class CustomerInterceptor extends HandlerInterceptorAdapter{ 
    @Override 
    public void postHandle(...){ 
     MultiReadHttpServletRequest req = (MultiReadHttpServletRequest) request; 
     ContentCachingResponseWrapper res = new ContentCachingResponseWrapper(response); 
     Byte[] body = res. getContentAsByteArray(); 
     ... 
    } 
} 

我身體是[]。

+0

你能分享更多的代碼?你如何使用ContentCachingResponseWrapper?正如javadoc說你應該調用'getContentAsByteArray()'方法來檢索內容 – Leffchik

+0

是的,我使用'getContentAsByteArray()',但我得到的內容與「」。 – linghu

+0

你能分享攔截器代碼嗎,問題出在攔截器部分。 –

回答

0

後幾天。我找到answer.In的CustomerDispatcherServlet我要補充responseWrapper.copyBodyToResponse()

CustomerDIspatcherServlet這樣的:

@Component("dispatcherServlet") 
public class FofDisPatcherServlet extends DispatcherServlet { 
    @Override 
    protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception { 
     MultiReadHttpServletRequest requestWrapper = null; 
     try { 
      requestWrapper = new MultiReadHttpServletRequest(request); 
      if (!(response instanceof ContentCachingResponseWrapper)) { 
       ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response); 
       super.doDispatch(requestWrapper, responseWrapper); 
       responseWrapper.copyBodyToResponse(); 
      }else { 
       super.doDispatch(requestWrapper, response); 
      } 
     } catch (Exception e) { 
      super.doDispatch(request, response); 
     } 
    } 
}  
1

試試這個:

@Component("dispatcherServlet") 
public class FofDisPatcherServlet extends DispatcherServlet { 
    @Override 
    protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception { 
    MultiReadHttpServletRequest requestWrapper = null; 
    try { 
     requestWrapper = new MultiReadHttpServletRequest(request); 
     super.doDispatch(requestWrapper, new ContentCachingResponseWrapper(request)); 
    } catch (Exception e) { 
     super.doDispatch(request,response); 
    } 
} 

}

public class CustomerInterceptor extends HandlerInterceptorAdapter{ 
@Override 
public void postHandle(..., HttpServletResponse response){ 
    if (response instanceof ContentCachingResponseWrapper) { 
     Byte[] body = ((ContentCachingResponseWrapper)response). getContentAsByteArray(); 
    } 
    ... 
} 

}

0

的錯誤是在你的代碼

public class CustomerInterceptor extends HandlerInterceptorAdapter{ 
    @Override 
    public void postHandle((HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView){ 
     MultiReadHttpServletRequest req = (MultiReadHttpServletRequest) request; 
     ContentCachingResponseWrapper res = new ContentCachingResponseWrapper(response); 
     Byte[] body = res. getContentAsByteArray(); 
     ... 
    } 
} 

你傳入請求ContentCachingResponseWrapper。

請參閱this問題非常類似的問題。

+0

哦,在我的代碼中,我使用response.in在我寫錯的問題上,抱歉。 – linghu