Last active
June 6, 2019 22:19
-
-
Save joeyslalom/5a09d1a1c2e927e8955f2d5f43fd98c4 to your computer and use it in GitHub Desktop.
Log requests and responses
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.nio.charset.StandardCharsets; | |
import java.util.List; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.boot.web.client.RestTemplateCustomizer; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.http.HttpRequest; | |
import org.springframework.http.client.BufferingClientHttpRequestFactory; | |
import org.springframework.http.client.ClientHttpRequestExecution; | |
import org.springframework.http.client.ClientHttpRequestFactory; | |
import org.springframework.http.client.ClientHttpRequestInterceptor; | |
import org.springframework.http.client.ClientHttpResponse; | |
@Configuration | |
public class RestTemplateConfig { | |
@Bean | |
LoggingInterceptor loggingInterceptor() { | |
return new LoggingInterceptor(); | |
} | |
@Bean | |
RestTemplateCustomizer restTemplateCustomizer() { | |
return restTemplate -> { | |
ClientHttpRequestFactory requestFactory = restTemplate.getRequestFactory(); | |
restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(requestFactory)); | |
List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors(); | |
interceptors.add(loggingInterceptor()); | |
restTemplate.setInterceptors(interceptors); | |
}; | |
} | |
static class LoggingInterceptor implements ClientHttpRequestInterceptor { | |
private static final Logger LOG = LoggerFactory.getLogger(LoggingInterceptor.class); | |
@Override | |
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) | |
throws IOException { | |
traceRequest(request, body); | |
ClientHttpResponse response = execution.execute(request, body); | |
traceResponse(response); | |
return response; | |
} | |
private static void traceRequest(HttpRequest request, byte[] body) { | |
LOG.info("===========================request begin============================================="); | |
LOG.debug("URI : {}", request.getURI()); | |
LOG.debug("Method : {}", request.getMethod()); | |
LOG.debug("Headers: {}", request.getHeaders()); | |
LOG.debug("Body : {}", new String(body, StandardCharsets.UTF_8)); | |
LOG.info("===========================request end==============================================="); | |
} | |
private static void traceResponse(ClientHttpResponse response) throws IOException { | |
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(); | |
} | |
LOG.info("==========================response begin============================================="); | |
LOG.debug("Code : {}", response.getStatusCode()); | |
LOG.debug("Text : {}", response.getStatusText()); | |
LOG.debug("Headers: {}", response.getHeaders()); | |
LOG.debug("Body : {}", inputStringBuilder.toString()); | |
LOG.info("==========================response end==============================================="); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment