Skip to content

Commit 3c28681

Browse files
tinomthomaspedja4
authored andcommitted
BAEL-1756 (eugenp#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 (eugenp#4232) * Spring annotations * commented VehicleFactoryApplication to fix CI build * BAEL-1756 Test typo fixed
1 parent 506962b commit 3c28681

4 files changed

Lines changed: 120 additions & 0 deletions

File tree

‎spring-rest/src/main/java/com/baeldung/transfer/LoginForm.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ public class LoginForm {
77
public LoginForm() {
88
}
99

10+
public LoginForm(String username, String password) {
11+
super();
12+
this.username = username;
13+
this.password = password;
14+
}
15+
1016
public String getUsername() {
1117
return username;
1218
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.baeldung.config;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import org.baeldung.interceptors.RestTemplateLoggingInterceptor;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.context.annotation.Configuration;
9+
import org.springframework.http.client.BufferingClientHttpRequestFactory;
10+
import org.springframework.http.client.ClientHttpRequestInterceptor;
11+
import org.springframework.http.client.SimpleClientHttpRequestFactory;
12+
import org.springframework.util.CollectionUtils;
13+
import org.springframework.web.client.RestTemplate;
14+
15+
@Configuration
16+
public class RestClientConfig {
17+
18+
@Bean
19+
public RestTemplate restTemplate() {
20+
RestTemplate restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory()));
21+
22+
List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
23+
if (CollectionUtils.isEmpty(interceptors)) {
24+
interceptors = new ArrayList<ClientHttpRequestInterceptor>();
25+
}
26+
interceptors.add(new RestTemplateLoggingInterceptor());
27+
restTemplate.setInterceptors(interceptors);
28+
return restTemplate;
29+
}
30+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.baeldung.interceptors;
2+
3+
import java.io.IOException;
4+
import java.nio.charset.StandardCharsets;
5+
6+
import org.apache.commons.lang3.StringUtils;
7+
import org.springframework.http.HttpRequest;
8+
import org.springframework.http.client.ClientHttpRequestExecution;
9+
import org.springframework.http.client.ClientHttpRequestInterceptor;
10+
import org.springframework.http.client.ClientHttpResponse;
11+
import org.springframework.util.StreamUtils;
12+
13+
public class RestTemplateLoggingInterceptor implements ClientHttpRequestInterceptor {
14+
15+
@Override
16+
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
17+
logRequest(body);
18+
ClientHttpResponse response = execution.execute(request, body);
19+
logResponse(response);
20+
response.getHeaders().add("Foo", "bar");
21+
return response;
22+
}
23+
24+
private void logRequest(byte[] body) {
25+
String payLoad = StringUtils.EMPTY;
26+
if (body.length > 0) {
27+
payLoad = new String(body, StandardCharsets.UTF_8);
28+
}
29+
System.out.println("Request Body > " + payLoad);
30+
}
31+
32+
private void logResponse(ClientHttpResponse response) throws IOException {
33+
String payLoad = StringUtils.EMPTY;
34+
long contentLength = response.getHeaders()
35+
.getContentLength();
36+
if (contentLength != 0) {
37+
payLoad = StreamUtils.copyToString(response.getBody(), StandardCharsets.UTF_8);
38+
}
39+
System.out.println("Response Body > " + payLoad);
40+
}
41+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.baeldung.resttemplate;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.CoreMatchers.is;
5+
import static org.junit.Assert.assertThat;
6+
7+
import org.baeldung.config.RestClientConfig;
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.http.HttpEntity;
12+
import org.springframework.http.HttpHeaders;
13+
import org.springframework.http.HttpStatus;
14+
import org.springframework.http.MediaType;
15+
import org.springframework.http.ResponseEntity;
16+
import org.springframework.test.context.ContextConfiguration;
17+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
18+
import org.springframework.web.client.RestTemplate;
19+
20+
import com.baeldung.transfer.LoginForm;
21+
22+
@RunWith(SpringJUnit4ClassRunner.class)
23+
@ContextConfiguration(classes = RestClientConfig.class)
24+
public class RestTemplateIntegrationTest {
25+
26+
@Autowired
27+
RestTemplate restTemplate;
28+
29+
@Test
30+
public void givenRestTemplate_whenRequested_thenLogAndModifyResponse() {
31+
LoginForm loginForm = new LoginForm("userName", "password");
32+
HttpHeaders headers = new HttpHeaders();
33+
headers.setContentType(MediaType.APPLICATION_JSON);
34+
HttpEntity<LoginForm> requestEntity = new HttpEntity<LoginForm>(loginForm, headers);
35+
36+
ResponseEntity<String> responseEntity = restTemplate.postForEntity("http://httpbin.org/post", requestEntity, String.class);
37+
38+
assertThat(responseEntity.getStatusCode(), is(equalTo(HttpStatus.OK)));
39+
assertThat(responseEntity.getHeaders()
40+
.get("Foo")
41+
.get(0), is(equalTo("bar")));
42+
}
43+
}

0 commit comments

Comments
 (0)