commit
969ae2bd2e
|
@ -1,7 +1,6 @@
|
|||
package com.baeldung.resttemplate;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.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 {
|
||||
|
||||
final static Logger log = LoggerFactory.getLogger(LoggingInterceptor.class);
|
||||
final static Logger LOGGER = LoggerFactory.getLogger(LoggingInterceptor.class);
|
||||
|
||||
@Override
|
||||
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);
|
||||
InputStreamReader isr = new InputStreamReader(response.getBody(), StandardCharsets.UTF_8);
|
||||
String body = new BufferedReader(isr).lines()
|
||||
.collect(Collectors.joining("\n"));
|
||||
log.debug("Response body: {}", body);
|
||||
if (LOGGER.isDebugEnabled()) {
|
||||
InputStreamReader isr = new InputStreamReader(response.getBody(), StandardCharsets.UTF_8);
|
||||
String body = new BufferedReader(isr).lines()
|
||||
.collect(Collectors.joining("\n"));
|
||||
LOGGER.debug("Response body: {}", body);
|
||||
}
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,18 +1,21 @@
|
|||
package com.baeldung.resttemplate.logging;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.client.ClientHttpRequestFactory;
|
||||
import org.springframework.http.client.ClientHttpRequestInterceptor;
|
||||
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.util.CollectionUtils;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
@ -22,6 +25,7 @@ import org.springframework.web.client.RestTemplate;
|
|||
public class RestTemplateLoggingLiveTest {
|
||||
|
||||
private static final String baseUrl = "http://localhost:8080/spring-rest";
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(RestTemplateLoggingLiveTest.class);
|
||||
|
||||
@Test
|
||||
public void givenHttpClientConfiguration_whenSendGetForRequestEntity_thenRequestResponseFullLog() {
|
||||
|
@ -30,13 +34,22 @@ public class RestTemplateLoggingLiveTest {
|
|||
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
|
||||
|
||||
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
|
||||
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();
|
||||
if (CollectionUtils.isEmpty(interceptors)) {
|
||||
interceptors = new ArrayList<>();
|
||||
|
@ -45,6 +58,7 @@ public class RestTemplateLoggingLiveTest {
|
|||
restTemplate.setInterceptors(interceptors);
|
||||
|
||||
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.com.baeldung.resttemplate.logging.LoggingInterceptor=DEBUG
|
||||
logging.level.com.baeldung.resttemplate.logging=DEBUG
|
||||
logging.level.org.apache.http=DEBUG
|
||||
logging.level.httpclient.wire=DEBUG
|
||||
logging.pattern.console=%20logger{20} - %msg%n
|
||||
|
|
Loading…
Reference in New Issue