From 3c28681b6db356eebe3b2661c719fb7921c87606 Mon Sep 17 00:00:00 2001 From: Tino Mulanchira Thomas <38363530+tinomthomas@users.noreply.github.com> Date: Sun, 13 May 2018 11:31:30 +0300 Subject: [PATCH] BAEL-1756 (#4230) * code samples for spring data key value BAEL-1467 * changes to add spring-data-keyvalue to parent pom. * Update README.md * Update README.md * Update README.md * How to Use Spring RestTemplate Interceptor * Spring annotations article (#4232) * Spring annotations * commented VehicleFactoryApplication to fix CI build * BAEL-1756 Test typo fixed --- .../java/com/baeldung/transfer/LoginForm.java | 6 +++ .../org/baeldung/config/RestClientConfig.java | 30 +++++++++++++ .../RestTemplateLoggingInterceptor.java | 41 ++++++++++++++++++ .../RestTemplateIntegrationTest.java | 43 +++++++++++++++++++ 4 files changed, 120 insertions(+) create mode 100644 spring-rest/src/main/java/org/baeldung/config/RestClientConfig.java create mode 100644 spring-rest/src/main/java/org/baeldung/interceptors/RestTemplateLoggingInterceptor.java create mode 100644 spring-rest/src/test/java/org/baeldung/resttemplate/RestTemplateIntegrationTest.java diff --git a/spring-rest/src/main/java/com/baeldung/transfer/LoginForm.java b/spring-rest/src/main/java/com/baeldung/transfer/LoginForm.java index 19c9b0a349..caafcdb500 100644 --- a/spring-rest/src/main/java/com/baeldung/transfer/LoginForm.java +++ b/spring-rest/src/main/java/com/baeldung/transfer/LoginForm.java @@ -7,6 +7,12 @@ public class LoginForm { public LoginForm() { } + public LoginForm(String username, String password) { + super(); + this.username = username; + this.password = password; + } + public String getUsername() { return username; } diff --git a/spring-rest/src/main/java/org/baeldung/config/RestClientConfig.java b/spring-rest/src/main/java/org/baeldung/config/RestClientConfig.java new file mode 100644 index 0000000000..3619f43f8a --- /dev/null +++ b/spring-rest/src/main/java/org/baeldung/config/RestClientConfig.java @@ -0,0 +1,30 @@ +package org.baeldung.config; + +import java.util.ArrayList; +import java.util.List; + +import org.baeldung.interceptors.RestTemplateLoggingInterceptor; +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; + +@Configuration +public class RestClientConfig { + + @Bean + public RestTemplate restTemplate() { + RestTemplate restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory())); + + List interceptors = restTemplate.getInterceptors(); + if (CollectionUtils.isEmpty(interceptors)) { + interceptors = new ArrayList(); + } + interceptors.add(new RestTemplateLoggingInterceptor()); + restTemplate.setInterceptors(interceptors); + return restTemplate; + } +} diff --git a/spring-rest/src/main/java/org/baeldung/interceptors/RestTemplateLoggingInterceptor.java b/spring-rest/src/main/java/org/baeldung/interceptors/RestTemplateLoggingInterceptor.java new file mode 100644 index 0000000000..56de3d04b8 --- /dev/null +++ b/spring-rest/src/main/java/org/baeldung/interceptors/RestTemplateLoggingInterceptor.java @@ -0,0 +1,41 @@ +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) { + String payLoad = StringUtils.EMPTY; + if (body.length > 0) { + payLoad = new String(body, StandardCharsets.UTF_8); + } + System.out.println("Request Body > " + payLoad); + } + + private void logResponse(ClientHttpResponse response) throws IOException { + String payLoad = StringUtils.EMPTY; + long contentLength = response.getHeaders() + .getContentLength(); + if (contentLength != 0) { + payLoad = StreamUtils.copyToString(response.getBody(), StandardCharsets.UTF_8); + } + System.out.println("Response Body > " + payLoad); + } +} diff --git a/spring-rest/src/test/java/org/baeldung/resttemplate/RestTemplateIntegrationTest.java b/spring-rest/src/test/java/org/baeldung/resttemplate/RestTemplateIntegrationTest.java new file mode 100644 index 0000000000..e0c24c32b1 --- /dev/null +++ b/spring-rest/src/test/java/org/baeldung/resttemplate/RestTemplateIntegrationTest.java @@ -0,0 +1,43 @@ +package org.baeldung.resttemplate; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import org.baeldung.config.RestClientConfig; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.web.client.RestTemplate; + +import com.baeldung.transfer.LoginForm; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = RestClientConfig.class) +public class RestTemplateIntegrationTest { + + @Autowired + RestTemplate restTemplate; + + @Test + public void givenRestTemplate_whenRequested_thenLogAndModifyResponse() { + LoginForm loginForm = new LoginForm("userName", "password"); + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + HttpEntity requestEntity = new HttpEntity(loginForm, headers); + + ResponseEntity responseEntity = restTemplate.postForEntity("http://httpbin.org/post", requestEntity, String.class); + + assertThat(responseEntity.getStatusCode(), is(equalTo(HttpStatus.OK))); + assertThat(responseEntity.getHeaders() + .get("Foo") + .get(0), is(equalTo("bar"))); + } +}