我已經實現了以下方法,以測井響應,但將在IOUtils.toString異常(responseWrapper.getContentInputStream(),「UTF-8」);說響應有零字節。我能夠訪問標題和狀態。我怎樣才能得到響應體?在彈簧測井響應內容HandlerInterceptorAdapter的postHandle
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
System.out.println("after Request");
ObjectMapper objectMapper = new ObjectMapper();
ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response);
HttpStatus responseStatus = HttpStatus.valueOf(responseWrapper.getStatusCode());
System.out.println(responseWrapper.getStatusCode());
HttpHeaders responseHeaders = new HttpHeaders();
for (String headerName : responseWrapper.getHeaderNames()) {
System.out.println(headerName);
responseHeaders.add(headerName, responseWrapper.getHeader(headerName));
}
String responseBody = IOUtils.toString(responseWrapper.getContentInputStream(), "UTF-8");
System.out.println(responseBody);
log.info("Response body {} ",responseBody);
responseWrapper.copyBodyToResponse();
}
這是在談論過濾器,但我使用的春天HandlerInterceptorAdapter。 – user2478236
同樣的推理適用。這裏的關鍵是您無法訪問處理響應的輸出流。你需要複製它。 –