This commit is contained in:
Tino Mulanchira Thomas 2018-05-28 01:48:42 +03:00 committed by Predrag Maric
parent db233245e3
commit 0f15f15bf3
3 changed files with 21 additions and 44 deletions

View File

@ -3,12 +3,10 @@ package org.baeldung.config;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.baeldung.interceptors.RestTemplateLoggingInterceptor; import org.baeldung.interceptors.RestTemplateHeaderModifierInterceptor;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.BufferingClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpRequestInterceptor; import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
@ -17,13 +15,13 @@ public class RestClientConfig {
@Bean @Bean
public RestTemplate restTemplate() { public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory())); RestTemplate restTemplate = new RestTemplate();
List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors(); List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
if (CollectionUtils.isEmpty(interceptors)) { if (CollectionUtils.isEmpty(interceptors)) {
interceptors = new ArrayList<ClientHttpRequestInterceptor>(); interceptors = new ArrayList<ClientHttpRequestInterceptor>();
} }
interceptors.add(new RestTemplateLoggingInterceptor()); interceptors.add(new RestTemplateHeaderModifierInterceptor());
restTemplate.setInterceptors(interceptors); restTemplate.setInterceptors(interceptors);
return restTemplate; return restTemplate;
} }

View File

@ -0,0 +1,18 @@
package org.baeldung.interceptors;
import java.io.IOException;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
public class RestTemplateHeaderModifierInterceptor implements ClientHttpRequestInterceptor {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
ClientHttpResponse response = execution.execute(request, body);
response.getHeaders().add("Foo", "bar");
return response;
}
}

View File

@ -1,39 +0,0 @@
package org.baeldung.interceptors;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import org.apache.commons.lang3.StringUtils;
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.util.StreamUtils;
public class RestTemplateLoggingInterceptor implements ClientHttpRequestInterceptor {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
logRequest(body);
ClientHttpResponse response = execution.execute(request, body);
logResponse(response);
response.getHeaders().add("Foo", "bar");
return response;
}
private void logRequest(byte[] body) {
if (body.length > 0) {
String payLoad = new String(body, StandardCharsets.UTF_8);
System.out.println(payLoad);
}
}
private void logResponse(ClientHttpResponse response) throws IOException {
long contentLength = response.getHeaders()
.getContentLength();
if (contentLength != 0) {
String payLoad = StreamUtils.copyToString(response.getBody(), StandardCharsets.UTF_8);
System.out.println(payLoad);
}
}
}