简体   繁体   English

使用Spring RestTemplate,出现错误时如何记录响应?

[英]Using Spring RestTemplate, how can I log the response when there is an error?

Spring RestTemplate could set the errorHandler, which has the methods for handling errors. Spring RestTemplate可以设置errorHandler,它具有处理错误的方法。 I have an error handler like this 我有这样的错误处理程序

try (BufferedReader buffer = new BufferedReader(new InputStreamReader(clientHttpResponse.getBody()))) {
     final String response = buffer.lines().collect(Collectors.joining("\n"));
     System.out.println(response);
  }

but it always tell me that the input stream is close. 但是它总是告诉我输入流是关闭的。 Is this a bug or do I missing something? 这是错误还是我错过了什么?

The default error handler of RestTemplate DefaultResponseErrorHandler already reads the response body and sets it in the HttpStatusCodeException object that it throws. RestTemplate的默认错误处理程序DefaultResponseErrorHandler已经读取响应主体,并将其设置在HttpStatusCodeException它抛出对象。

try {
    restTemplate.getForObject("http://...", String.class);
} catch (HttpStatusCodeException e) {
    System.out.println("Received error: " + e.getResponseBodyAsString());
}

If you want to always log the response body on error without requiring to catch HttpStatusCodeException , you can extend the default error handler. 如果您想始终在错误上记录响应正文而无需捕获HttpStatusCodeException ,则可以扩展默认错误处理程序。

public static class LoggingErrorHandler extends DefaultResponseErrorHandler {
    @Override
    public void handleError(ClientHttpResponse response) throws IOException {
        try {
            super.handleError(response);
        } catch (HttpStatusCodeException e) {
            System.out.println("Error response body is " + e.getResponseBodyAsString());
            throw e;
        }
    }
}

Use the above error handler while instantiating rest template. 在实例化其余模板时使用上面的错误处理程序。

RestTemplate restTemplate = new RestTemplate();
restTemplate.setErrorHandler(new LoggingErrorHandler());
String response = restTemplate.getForObject("http://...", String.class);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring MVC3发送HTTP错误响应(例如400)时,如何注销一些详细信息? - How can I log out some details when Spring MVC3 sends an HTTP error response such as 400? 如何使用 Builder Pattern 和 @JsonDeserialize 使用 Spring RestTemplate 反序列化响应 json - How to deserialize response json with Spring RestTemplate using Builder Pattern and @JsonDeserialize Spring RestTemplate 解析自定义错误响应 - Spring RestTemplate Parse Custom Error Response 如何在HandlerInterceptorAdapter中使用@ResponseBody记录Spring 3控制器的JSON响应? - How can I log the JSON response of Spring 3 controllers with @ResponseBody in a HandlerInterceptorAdapter? 如何在Spring中使用RestTemplate发布到REST服务 - How do I post to a REST service using RestTemplate in Spring 对于复杂的 RestTemplate Body Object 需要帮助。 如何使用 RestTemplate 构建它? - Need help for complicated RestTemplate Body Object. How can I build this using RestTemplate? 如何在 Spring-Web 中使用 RestTemplate 解析 gzip 编码的响应 - How to parse gzip encoded response with RestTemplate in Spring-Web 如何使用Spring框架设置自定义响应短语? - How can i set custom response phrase using Spring framework? 如何使用log4j2在日志文件中记录spring JdbcTemplate sql查询和数据库响应 - How to log spring JdbcTemplate sql queries and DB response in log file using log4j2 创建自定义RestTemplate时,Spring-boot会出错 - Spring-boot gets an error when creating custom RestTemplate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM