2017년 2월 10일 금요일

RestTemplate Logging

You can set the restTemplate logging interceptor by ClientHttpRequestInterceptor.


- Structure




- RestTemplateConfig.java

package org.blog.test.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.web.client.RestTemplate;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Configuration
@Slf4j
public class RestTemplateConfig {

    @Bean
    public RestTemplate restTemplate() {
        RestTemplate restTemplate = new RestTemplate();

        List interceptors = new ArrayList<>();
        interceptors.add(new ClientHttpRequestInterceptor() {
            @Override
            public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
                ClientHttpResponse response = execution.execute(request, body);
                doLog(request, body, response);
                return response;
            }

            private void doLog(HttpRequest request, byte[] body, ClientHttpResponse response) throws IOException {
                log.info("[Request] [{}]  {}, Headers : [{}], Body : [{}]", request.getMethod(), request.getURI(), request.getHeaders(), new String(body, Charset.forName("UTF-8")));
                log.info("[Response] [{}], Headers : [{}], Body : [{}] ", response.getStatusCode(), response.getHeaders(), new BufferedReader(new InputStreamReader(response.getBody())).readLine())
                ;
            }
        });
        restTemplate.setInterceptors(interceptors);
        return restTemplate;
    }
}

- RestCallServiceImpl.java

package org.blog.test.restcall.service.impl;

import org.blog.test.restcall.model.TestRequest;
import org.blog.test.restcall.service.RestCallService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.lang.reflect.Type;
import java.nio.charset.Charset;

@Service
public class RestCallServiceImpl implements RestCallService {

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public String getTestString() {
        ResponseEntity responseEntity = restTemplate.exchange("http://google.com", HttpMethod.GET, null, String.class);
        return responseEntity.getBody();
    }
}


- Result

2017-02-10 17:10:48.078  INFO 4432 --- [nio-8080-exec-1] org.blog.test.config.RestTemplateConfig  : [Request] [GET]  http://google.com, Headers : [{Accept=[text/plain, application/json, application/*+json, */*], Content-Length=[0]}], Body : []
2017-02-10 17:10:48.092  INFO 4432 --- [nio-8080-exec-1] org.blog.test.config.RestTemplateConfig  : [Response] [200], Headers : [{Date=[Fri, 10 Feb 2017 08:10:12 GMT], Expires=[-1], Cache-Control=[private, max-age=0], Content-Type=[text/html; charset=EUC-KR], P3P=[CP="This is not a P3P policy! See https://www.google.com/support/accounts/answer/151657?hl=en for more info."], Server=[gws], X-XSS-Protection=[1; mode=block], X-Frame-Options=[SAMEORIGIN], Set-Cookie=[NID=96=b2LgtIg7PncSQLK0OeCPOKcSwmpWyMJN_3SU6QSKQ30UbUQ3jTIk5TCFgGgbf-rYt-nlWjSZzGoyfnTywJOkmavt3WDqyVexDSxT8rOLHn3-eqDiK6Cps5hYKckQCzsD; expires=Sat, 12-Aug-2017 08:10:12 GMT; path=/; domain=.google.co.kr; HttpOnly], Accept-Ranges=[none], Vary=[Accept-Encoding], Transfer-Encoding=[chunked]}], Body : [.....]

댓글 없음 :

댓글 쓰기