commit
969ae2bd2e
@ -1,7 +1,6 @@
|
|||||||
package com.baeldung.resttemplate;
|
package com.baeldung.resttemplate;
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
|
@ -1,14 +0,0 @@
|
|||||||
package com.baeldung.resttemplate.logging;
|
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
|
||||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|
||||||
|
|
||||||
@SpringBootApplication
|
|
||||||
@EnableAutoConfiguration
|
|
||||||
public class RestTemplateConfigurationApplication {
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
SpringApplication.run(RestTemplateConfigurationApplication.class, args);
|
|
||||||
}
|
|
||||||
}
|
|
@ -14,16 +14,18 @@ import org.springframework.http.client.ClientHttpResponse;
|
|||||||
|
|
||||||
public class LoggingInterceptor implements ClientHttpRequestInterceptor {
|
public class LoggingInterceptor implements ClientHttpRequestInterceptor {
|
||||||
|
|
||||||
final static Logger log = LoggerFactory.getLogger(LoggingInterceptor.class);
|
final static Logger LOGGER = LoggerFactory.getLogger(LoggingInterceptor.class);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ClientHttpResponse intercept(HttpRequest req, byte[] reqBody, ClientHttpRequestExecution ex) throws IOException {
|
public ClientHttpResponse intercept(HttpRequest req, byte[] reqBody, ClientHttpRequestExecution ex) throws IOException {
|
||||||
log.debug("Request body: {}", new String(reqBody, StandardCharsets.UTF_8));
|
LOGGER.debug("Request body: {}", new String(reqBody, StandardCharsets.UTF_8));
|
||||||
ClientHttpResponse response = ex.execute(req, reqBody);
|
ClientHttpResponse response = ex.execute(req, reqBody);
|
||||||
|
if (LOGGER.isDebugEnabled()) {
|
||||||
InputStreamReader isr = new InputStreamReader(response.getBody(), StandardCharsets.UTF_8);
|
InputStreamReader isr = new InputStreamReader(response.getBody(), StandardCharsets.UTF_8);
|
||||||
String body = new BufferedReader(isr).lines()
|
String body = new BufferedReader(isr).lines()
|
||||||
.collect(Collectors.joining("\n"));
|
.collect(Collectors.joining("\n"));
|
||||||
log.debug("Response body: {}", body);
|
LOGGER.debug("Response body: {}", body);
|
||||||
|
}
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,18 +1,21 @@
|
|||||||
package com.baeldung.resttemplate.logging;
|
package com.baeldung.resttemplate.logging;
|
||||||
|
|
||||||
import static org.hamcrest.CoreMatchers.equalTo;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
import static org.hamcrest.MatcherAssert.assertThat;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.http.client.ClientHttpRequestFactory;
|
||||||
import org.springframework.http.client.ClientHttpRequestInterceptor;
|
import org.springframework.http.client.ClientHttpRequestInterceptor;
|
||||||
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
||||||
|
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
||||||
|
import org.springframework.http.client.BufferingClientHttpRequestFactory;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
@ -22,6 +25,7 @@ import org.springframework.web.client.RestTemplate;
|
|||||||
public class RestTemplateLoggingLiveTest {
|
public class RestTemplateLoggingLiveTest {
|
||||||
|
|
||||||
private static final String baseUrl = "http://localhost:8080/spring-rest";
|
private static final String baseUrl = "http://localhost:8080/spring-rest";
|
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(RestTemplateLoggingLiveTest.class);
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenHttpClientConfiguration_whenSendGetForRequestEntity_thenRequestResponseFullLog() {
|
public void givenHttpClientConfiguration_whenSendGetForRequestEntity_thenRequestResponseFullLog() {
|
||||||
@ -30,13 +34,22 @@ public class RestTemplateLoggingLiveTest {
|
|||||||
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
|
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
|
||||||
|
|
||||||
final ResponseEntity<String> response = restTemplate.postForEntity(baseUrl + "/persons", "my request body", String.class);
|
final ResponseEntity<String> response = restTemplate.postForEntity(baseUrl + "/persons", "my request body", String.class);
|
||||||
assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
|
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||||
|
assertEquals("[\"Lucie\",\"Jackie\",\"Danesh\",\"Tao\"]", response.getBody());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenLoggingInterceptorConfiguration_whenSendGetForRequestEntity_thenRequestResponseCustomLog() {
|
public void givenLoggingInterceptorConfiguration_whenSendGetForRequestEntity_thenRequestResponseCustomLog() {
|
||||||
|
|
||||||
RestTemplate restTemplate = new RestTemplate();
|
RestTemplate restTemplate = null;
|
||||||
|
if (LOGGER.isDebugEnabled()) {
|
||||||
|
ClientHttpRequestFactory factory = new BufferingClientHttpRequestFactory(
|
||||||
|
new SimpleClientHttpRequestFactory());
|
||||||
|
restTemplate = new RestTemplate(factory);
|
||||||
|
} else {
|
||||||
|
restTemplate = new RestTemplate();
|
||||||
|
}
|
||||||
|
|
||||||
List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
|
List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
|
||||||
if (CollectionUtils.isEmpty(interceptors)) {
|
if (CollectionUtils.isEmpty(interceptors)) {
|
||||||
interceptors = new ArrayList<>();
|
interceptors = new ArrayList<>();
|
||||||
@ -45,6 +58,7 @@ public class RestTemplateLoggingLiveTest {
|
|||||||
restTemplate.setInterceptors(interceptors);
|
restTemplate.setInterceptors(interceptors);
|
||||||
|
|
||||||
final ResponseEntity<String> response = restTemplate.postForEntity(baseUrl + "/persons", "my request body", String.class);
|
final ResponseEntity<String> response = restTemplate.postForEntity(baseUrl + "/persons", "my request body", String.class);
|
||||||
assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
|
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||||
|
assertEquals("[\"Lucie\",\"Jackie\",\"Danesh\",\"Tao\"]", response.getBody());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
logging.level.org.springframework.web.client.RestTemplate=DEBUG
|
logging.level.org.springframework.web.client.RestTemplate=DEBUG
|
||||||
logging.level.com.baeldung.resttemplate.logging.LoggingInterceptor=DEBUG
|
logging.level.com.baeldung.resttemplate.logging=DEBUG
|
||||||
logging.level.org.apache.http=DEBUG
|
logging.level.org.apache.http=DEBUG
|
||||||
logging.level.httpclient.wire=DEBUG
|
logging.level.httpclient.wire=DEBUG
|
||||||
logging.pattern.console=%20logger{20} - %msg%n
|
logging.pattern.console=%20logger{20} - %msg%n
|
||||||
|
Loading…
x
Reference in New Issue
Block a user