如何使用Spring记录RestTemplate请求和响应?

13

我正在使用 RestTemplate 和 Spring 执行 HTTP GET 请求。

如何在每个请求时自动记录日志文件中的请求和响应数据?


这并没有帮助,因为当我拦截消息时,我会得到类型为 org.springframework.http.client.InterceptingClientHttpRequestorg.springframework.http.client.SimpleClientHttpResponse 的 Java 对象。我需要的是响应的文本/XML表示形式,并将其记录下来。 - membersound
2
你尝试过将Apache HttpClient传递给RestTemplate发送消息并在HttpClient级别启用日志记录吗? - Giovanni
阅读我从以下位置给出的答案:如何使用RestTemplate记录请求和响应rest调用 - Solanki Vaibhav
请阅读我对以下问题的回答:answer - Solanki Vaibhav
1个回答

11
您可以使用Spring中的ClientHttpRequestInterceptor来实现这一点。您需要覆盖接口ClientHttpRequestInterceptor的方法intercept
以下是代码片段:
@Component
public class LogRequestResponseFilter implements ClientHttpRequestInterceptor {

private static final Logger logger=LoggerFactory.getLogger(LogRequestResponseFilter.class);

   @Override
   public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
         throws IOException {

      traceRequest(request, body);
      ClientHttpResponse clientHttpResponse = execution.execute(request, body);
      traceResponse(clientHttpResponse);

      return clientHttpResponse;
   } 

 private void traceRequest(HttpRequest request, byte[] body) throws IOException {
      logger.debug("request URI : " + request.getURI());
      logger.debug("request method : " + request.getMethod());
      logger.debug("request body : " + getRequestBody(body));
   }

   private String getRequestBody(byte[] body) throws UnsupportedEncodingException {
      if (body != null && body.length > 0) {
         return (new String(body, "UTF-8"));
      } else {
         return null;
      }
   }


  private void traceResponse(ClientHttpResponse response) throws IOException {
      String body = getBodyString(response);
      logger.debug("response status code: " + response.getStatusCode());
      logger.debug("response status text: " + response.getStatusText());
      logger.debug("response body : " + body);
   }

   private String getBodyString(ClientHttpResponse response) {
      try {
         if (response != null && response.getBody() != null) {// &&
                                                              // isReadableResponse(response))
                                                              // {
            StringBuilder inputStringBuilder = new StringBuilder();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getBody(), StandardCharsets.UTF_8));
            String line = bufferedReader.readLine();
            while (line != null) {
               inputStringBuilder.append(line);
               inputStringBuilder.append('\n');
               line = bufferedReader.readLine();
            }
            return inputStringBuilder.toString();
         } else {
            return null;
         }
      } catch (IOException e) {
         logger.error(e.getMessage(), e);
         return null;
      }
   }

只添加@Component到自定义的Interceptor是否足够?还是我必须在某个地方显式注册它,例如在RestTemplate上?因为这就是我想要避免的:不得不自己注册和连接。 - membersound
我相信@Component已经足够了。您可以按照下面的链接进行示例,这是一个很好的来源。http://blog.cacoveanu.com/2016/06/24/rest-template-logging.html - bvyas
4
这个日志记录正确,但是如果底层的InputStream不支持标记怎么办?一旦读取完成就会消失。 - Captain Man
我在使用这个时遇到了一个错误,跟踪响应会防止它后来被编组成我的POJO。不确定为什么,但对于调试仍然有帮助。 - IcedDante

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接