RestTemplate设置了readtimeout,并且也请求响应日志记录

时间:2019-02-27 09:20:13

标签: java spring spring-boot spring-mvc resttemplate

我推荐了许多SO链接来进行以下操作。

  1. RestTemplate设置读取和连接超时
  2. RestTemplate请求响应日志记录

为此,我尝试使用以下代码。

尝试1:设置读取和连接超时,我尝试了以下解决方案。

@Component
public class RestTemplateInitializer implements ApplicationListener<ApplicationReadyEvent> {

    @Value("${my.api.timeout}")
    private int myTimeOut;

    public static HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory();

    @Override
    public void onApplicationEvent(ApplicationReadyEvent arg0) {
        clientHttpRequestFactory.setConnectTimeout(myTimeOut);
        clientHttpRequestFactory.setReadTimeout(myTimeOut);
    }


}

要创建RestTemplate,我尝试如下操作。

RestTemplate restTemplate = new RestTemplate(RestTemplateInitializer.clientHttpRequestFactory);

尝试2:请求响应日志记录。

public class RequestResponseLoggingInterceptor implements ClientHttpRequestInterceptor {

    private LoggerUtil log = LoggerUtil.getInstance();

    private final String className = "RequestResponseLoggingInterceptor";

    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException
    {
        logRequest(request, body);
        ClientHttpResponse response = execution.execute(request, body);
        logResponse(response);

        //Add optional additional hostHeaders
        response.getHeaders().add("headerName", "VALUE");

        return response;
    }

    private void logRequest(HttpRequest request, byte[] body) throws IOException {
        final String methodName = "logRequest";
        log.doLog(4, className, methodName, "===========================request begin================================================");
        log.doLog(4, className, methodName, "URI         : {}"+request.getURI());
        log.doLog(4, className, methodName, "Method      : {}"+request.getMethod());
        log.doLog(4, className, methodName, "Headers     : {}"+request.getHeaders());
        log.doLog(4, className, methodName, "Request body: {}"+new String(body, "UTF-8"));
        log.doLog(4, className, methodName, "==========================request end================================================");
    }

    private void logResponse(ClientHttpResponse response) throws IOException {
        final String methodName = "logResponse";
        log.doLog(4, className, methodName, "============================response begin==========================================");
        log.doLog(4, className, methodName, "Status code  : {}"+response.getStatusCode());
        log.doLog(4, className, methodName, "Status text  : {}"+response.getStatusText());
        log.doLog(4, className, methodName, "Headers      : {}"+response.getHeaders());
        log.doLog(4, className, methodName, "Response body: {}"+StreamUtils.copyToString(response.getBody(), Charset.defaultCharset()));
        log.doLog(4, className, methodName, "=======================response end=================================================");
    }
}

要创建RestTemplate,我尝试如下操作。

RestTemplate restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory()));
restTemplate.getInterceptors().add(new RequestResponseLoggingInterceptor());
restTemplate.getInterceptors().add(new BasicAuthenticationInterceptor("Test", "Test"));

以上两种解决方案的问题是我无法混合使用这两种解决方案。即我无法在执行请求响应日志记录时设置读取和连接超时,因为new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory())不允许我设置读取和连接超时。而且当我使用new RestTemplate(RestTemplateInitializer.clientHttpRequestFactory)时,我无法读取两次响应。

我们能同时解决这两个问题吗?提前致谢!!

欢呼编程!

0 个答案:

没有答案