BAEL-1756 (#4349)
This commit is contained in:
parent
db233245e3
commit
0f15f15bf3
|
@ -3,12 +3,10 @@ package org.baeldung.config;
|
|||
import java.util.ArrayList;
|
||||
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.Configuration;
|
||||
import org.springframework.http.client.BufferingClientHttpRequestFactory;
|
||||
import org.springframework.http.client.ClientHttpRequestInterceptor;
|
||||
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
|
@ -17,13 +15,13 @@ public class RestClientConfig {
|
|||
|
||||
@Bean
|
||||
public RestTemplate restTemplate() {
|
||||
RestTemplate restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory()));
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
|
||||
List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
|
||||
if (CollectionUtils.isEmpty(interceptors)) {
|
||||
interceptors = new ArrayList<ClientHttpRequestInterceptor>();
|
||||
}
|
||||
interceptors.add(new RestTemplateLoggingInterceptor());
|
||||
interceptors.add(new RestTemplateHeaderModifierInterceptor());
|
||||
restTemplate.setInterceptors(interceptors);
|
||||
return restTemplate;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue