BAEL-1766: Configure a RestTemplate with RestTemplateBuilder (#4319)
This commit is contained in:
parent
454171e047
commit
b6b060d0f5
|
@ -0,0 +1,32 @@
|
||||||
|
package org.baeldung.resttemplate.configuration;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* interceptor to log incoming requests
|
||||||
|
*/
|
||||||
|
public class CustomClientHttpRequestInterceptor implements ClientHttpRequestInterceptor {
|
||||||
|
|
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(CustomClientHttpRequestInterceptor.class);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
|
||||||
|
|
||||||
|
logRequestDetails(request);
|
||||||
|
|
||||||
|
return execution.execute(request, body);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void logRequestDetails(HttpRequest request) {
|
||||||
|
LOGGER.info("Request Headers: {}", request.getHeaders());
|
||||||
|
LOGGER.info("Request Method: {}", request.getMethod());
|
||||||
|
LOGGER.info("Request URI: {}", request.getURI());
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
package org.baeldung.resttemplate.configuration;
|
||||||
|
|
||||||
|
import org.springframework.boot.web.client.RestTemplateCustomizer;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* customize rest template with an interceptor
|
||||||
|
*/
|
||||||
|
public class CustomRestTemplateCustomizer implements RestTemplateCustomizer {
|
||||||
|
@Override
|
||||||
|
public void customize(RestTemplate restTemplate) {
|
||||||
|
restTemplate.getInterceptors().add(new CustomClientHttpRequestInterceptor());
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,37 @@
|
||||||
|
package org.baeldung.resttemplate.configuration;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.web.client.RestTemplateBuilder;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Controller to test RestTemplate configuration
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
public class HelloController {
|
||||||
|
|
||||||
|
private static final String RESOURCE_URL = "http://localhost:8082/spring-rest/baz";
|
||||||
|
private RestTemplate restTemplate;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public HelloController(RestTemplateBuilder builder) {
|
||||||
|
this.restTemplate = builder.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping("/foo")
|
||||||
|
public String foo() {
|
||||||
|
|
||||||
|
ResponseEntity<String> response = restTemplate.getForEntity(RESOURCE_URL, String.class);
|
||||||
|
|
||||||
|
return response.getBody();
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping("/baz")
|
||||||
|
public String baz() {
|
||||||
|
return "Foo";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
package org.baeldung.resttemplate.configuration;
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,28 @@
|
||||||
|
package org.baeldung.resttemplate.configuration;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
|
import org.springframework.boot.web.client.RestTemplateBuilder;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.DependsOn;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableAutoConfiguration
|
||||||
|
@ComponentScan("org.baeldung.resttemplate.configuration")
|
||||||
|
public class SpringConfig {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@Qualifier("customRestTemplateCustomizer")
|
||||||
|
public CustomRestTemplateCustomizer customRestTemplateCustomizer() {
|
||||||
|
return new CustomRestTemplateCustomizer();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@DependsOn(value = {"customRestTemplateCustomizer"})
|
||||||
|
public RestTemplateBuilder restTemplateBuilder() {
|
||||||
|
return new RestTemplateBuilder(customRestTemplateCustomizer());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue