From 565d4f32c2b8e8b9b24308f49b774b7b48aac5cc Mon Sep 17 00:00:00 2001 From: Benjamin Caure Date: Fri, 9 Oct 2020 12:53:19 +0200 Subject: [PATCH] BAEL-4328 --- .../RestTemplateConfigurationApplication.java | 1 - .../RestTemplateConfigurationApplication.java | 14 ---------- .../logging/LoggingInterceptor.java | 14 +++++----- .../logging/RestTemplateLoggingLiveTest.java | 26 ++++++++++++++----- .../src/test/resources/application.properties | 2 +- 5 files changed, 29 insertions(+), 28 deletions(-) delete mode 100644 spring-resttemplate-2/src/main/java/com/baeldung/resttemplate/logging/RestTemplateConfigurationApplication.java diff --git a/spring-resttemplate-2/src/main/java/com/baeldung/resttemplate/RestTemplateConfigurationApplication.java b/spring-resttemplate-2/src/main/java/com/baeldung/resttemplate/RestTemplateConfigurationApplication.java index 8df3c13d7b..a7e65fc96c 100644 --- a/spring-resttemplate-2/src/main/java/com/baeldung/resttemplate/RestTemplateConfigurationApplication.java +++ b/spring-resttemplate-2/src/main/java/com/baeldung/resttemplate/RestTemplateConfigurationApplication.java @@ -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 diff --git a/spring-resttemplate-2/src/main/java/com/baeldung/resttemplate/logging/RestTemplateConfigurationApplication.java b/spring-resttemplate-2/src/main/java/com/baeldung/resttemplate/logging/RestTemplateConfigurationApplication.java deleted file mode 100644 index 4fa14edda1..0000000000 --- a/spring-resttemplate-2/src/main/java/com/baeldung/resttemplate/logging/RestTemplateConfigurationApplication.java +++ /dev/null @@ -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); - } -} diff --git a/spring-resttemplate-2/src/test/java/com/baeldung/resttemplate/logging/LoggingInterceptor.java b/spring-resttemplate-2/src/test/java/com/baeldung/resttemplate/logging/LoggingInterceptor.java index 17ce390d8a..c699d9d9dc 100644 --- a/spring-resttemplate-2/src/test/java/com/baeldung/resttemplate/logging/LoggingInterceptor.java +++ b/spring-resttemplate-2/src/test/java/com/baeldung/resttemplate/logging/LoggingInterceptor.java @@ -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; } } diff --git a/spring-resttemplate-2/src/test/java/com/baeldung/resttemplate/logging/RestTemplateLoggingLiveTest.java b/spring-resttemplate-2/src/test/java/com/baeldung/resttemplate/logging/RestTemplateLoggingLiveTest.java index 99d0201eff..86ffa574a0 100644 --- a/spring-resttemplate-2/src/test/java/com/baeldung/resttemplate/logging/RestTemplateLoggingLiveTest.java +++ b/spring-resttemplate-2/src/test/java/com/baeldung/resttemplate/logging/RestTemplateLoggingLiveTest.java @@ -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 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 interceptors = restTemplate.getInterceptors(); if (CollectionUtils.isEmpty(interceptors)) { interceptors = new ArrayList<>(); @@ -45,6 +58,7 @@ public class RestTemplateLoggingLiveTest { restTemplate.setInterceptors(interceptors); final ResponseEntity 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()); } } diff --git a/spring-resttemplate-2/src/test/resources/application.properties b/spring-resttemplate-2/src/test/resources/application.properties index 7bc9e56041..286ea95a4f 100644 --- a/spring-resttemplate-2/src/test/resources/application.properties +++ b/spring-resttemplate-2/src/test/resources/application.properties @@ -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