首页 > 解决方案 > RestTemplate 请求失败时如何打印详细信息状态?

问题描述

我用resttemplate调用http://example/jsonObject之类的链接,大概一分钟4000次左右,大部分时候没问题,但是有时候resttemplate会在一分钟内抛出60次以上的异常。

restTemplate error org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://example/jsonObject": Read timed out; nested exception is java.net.SocketTimeoutException: Read timed out
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:751)

我使用代码:

    try {
        CouponV2ResultVO couponV2ResultVO = restTemplate.getForObject("http://example/jsonObject", CouponV2ResultVO.class);
        long expense = System.currentTimeMillis() - startMs;
        log.info("takes {} {} ms", expense);
        return couponV2ResultVO;
    } catch (Exception e) {
        long expense = System.currentTimeMillis() - startMs;
        log.error("takes {} ms", expense, couponReqFullUri, ExceptionUtils.getFullStackTrace(e));
        throw e;
    }

我想打印一个更完整的细节,例如当resttemplate失败时整个请求之间的tcp通信,或者任何其他使用信息而不是超时异常。这可能吗?

标签: springnetworkingtcp

解决方案


当您想了解有关异常的更多详细信息时,可以使用log.error(e)ore.printStackTrace代替。throw e


推荐阅读