diff --git a/spring-resttemplate-2/README.md b/spring-resttemplate-2/README.md new file mode 100644 index 0000000000..e0a394c642 --- /dev/null +++ b/spring-resttemplate-2/README.md @@ -0,0 +1,7 @@ +## Spring RestTemplate + +This module contains articles about Spring RestTemplate + +### Relevant Articles: + + diff --git a/spring-resttemplate-2/pom.xml b/spring-resttemplate-2/pom.xml new file mode 100644 index 0000000000..1a594fd21e --- /dev/null +++ b/spring-resttemplate-2/pom.xml @@ -0,0 +1,63 @@ + + + 4.0.0 + spring-resttemplate + 0.1-SNAPSHOT + spring-resttemplate-2 + war + + + com.baeldung + parent-boot-2 + 0.0.1-SNAPSHOT + ../parent-boot-2 + + + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-test + + + + + + org.springframework + spring-web + + + commons-logging + commons-logging + + + + + + + org.slf4j + slf4j-api + + + ch.qos.logback + logback-classic + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + 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 new file mode 100644 index 0000000000..4fa14edda1 --- /dev/null +++ b/spring-resttemplate-2/src/main/java/com/baeldung/resttemplate/logging/RestTemplateConfigurationApplication.java @@ -0,0 +1,14 @@ +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/main/java/com/baeldung/resttemplate/logging/web/controller/PersonController.java b/spring-resttemplate-2/src/main/java/com/baeldung/resttemplate/logging/web/controller/PersonController.java new file mode 100644 index 0000000000..8436c52a4a --- /dev/null +++ b/spring-resttemplate-2/src/main/java/com/baeldung/resttemplate/logging/web/controller/PersonController.java @@ -0,0 +1,17 @@ +package com.baeldung.resttemplate.logging.web.controller; + +import java.util.Arrays; +import java.util.List; + +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class PersonController { + + @PostMapping("/persons") + public List getPersons() { + return Arrays.asList(new String[] { "Lucie", "Jackie", "Danesh", "Tao" }); + } + +} \ No newline at end of file diff --git a/spring-resttemplate-2/src/main/resources/application.properties b/spring-resttemplate-2/src/main/resources/application.properties new file mode 100644 index 0000000000..ea4f7c866d --- /dev/null +++ b/spring-resttemplate-2/src/main/resources/application.properties @@ -0,0 +1,2 @@ +server.port=8080 +server.servlet.context-path=/spring-rest \ No newline at end of file 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 new file mode 100644 index 0000000000..17ce390d8a --- /dev/null +++ b/spring-resttemplate-2/src/test/java/com/baeldung/resttemplate/logging/LoggingInterceptor.java @@ -0,0 +1,29 @@ +package com.baeldung.resttemplate.logging; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.util.stream.Collectors; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +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 LoggingInterceptor implements ClientHttpRequestInterceptor { + + final static Logger log = 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)); + 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); + 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 new file mode 100644 index 0000000000..99d0201eff --- /dev/null +++ b/spring-resttemplate-2/src/test/java/com/baeldung/resttemplate/logging/RestTemplateLoggingLiveTest.java @@ -0,0 +1,50 @@ +package com.baeldung.resttemplate.logging; + +import static org.hamcrest.CoreMatchers.equalTo; + +import static org.hamcrest.MatcherAssert.assertThat; + +import java.util.ArrayList; +import java.util.List; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.http.client.ClientHttpRequestInterceptor; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.util.CollectionUtils; +import org.springframework.web.client.RestTemplate; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class RestTemplateLoggingLiveTest { + + private static final String baseUrl = "http://localhost:8080/spring-rest"; + + @Test + public void givenHttpClientConfiguration_whenSendGetForRequestEntity_thenRequestResponseFullLog() { + + RestTemplate restTemplate = new RestTemplate(); + restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory()); + + final ResponseEntity response = restTemplate.postForEntity(baseUrl + "/persons", "my request body", String.class); + assertThat(response.getStatusCode(), equalTo(HttpStatus.OK)); + } + + @Test + public void givenLoggingInterceptorConfiguration_whenSendGetForRequestEntity_thenRequestResponseCustomLog() { + + RestTemplate restTemplate = new RestTemplate(); + List interceptors = restTemplate.getInterceptors(); + if (CollectionUtils.isEmpty(interceptors)) { + interceptors = new ArrayList<>(); + } + interceptors.add(new LoggingInterceptor()); + restTemplate.setInterceptors(interceptors); + + final ResponseEntity response = restTemplate.postForEntity(baseUrl + "/persons", "my request body", String.class); + assertThat(response.getStatusCode(), equalTo(HttpStatus.OK)); + } +} diff --git a/spring-resttemplate-2/src/test/resources/application.properties b/spring-resttemplate-2/src/test/resources/application.properties new file mode 100644 index 0000000000..7bc9e56041 --- /dev/null +++ b/spring-resttemplate-2/src/test/resources/application.properties @@ -0,0 +1,5 @@ +logging.level.org.springframework.web.client.RestTemplate=DEBUG +logging.level.com.baeldung.resttemplate.logging.LoggingInterceptor=DEBUG +logging.level.org.apache.http=DEBUG +logging.level.httpclient.wire=DEBUG +logging.pattern.console=%20logger{20} - %msg%n