* 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
This commit is contained in:
Tino Mulanchira Thomas 2018-05-13 11:31:30 +03:00 committed by Predrag Maric
parent 506962bc96
commit 3c28681b6d
4 changed files with 120 additions and 0 deletions

View File

@ -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;
}

View File

@ -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<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
if (CollectionUtils.isEmpty(interceptors)) {
interceptors = new ArrayList<ClientHttpRequestInterceptor>();
}
interceptors.add(new RestTemplateLoggingInterceptor());
restTemplate.setInterceptors(interceptors);
return restTemplate;
}
}

View File

@ -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);
}
}

View File

@ -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<LoginForm> requestEntity = new HttpEntity<LoginForm>(loginForm, headers);
ResponseEntity<String> 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")));
}
}