diff --git a/httpClients/boot-http-proxy/src/main/java/com/example/rest/proxy/client/JsonPlaceholderService.java b/httpClients/boot-http-proxy/src/main/java/com/example/rest/proxy/client/JsonPlaceholderService.java index 55c8e48c6..dfa4972ca 100644 --- a/httpClients/boot-http-proxy/src/main/java/com/example/rest/proxy/client/JsonPlaceholderService.java +++ b/httpClients/boot-http-proxy/src/main/java/com/example/rest/proxy/client/JsonPlaceholderService.java @@ -24,5 +24,5 @@ public interface JsonPlaceholderService { Post updatePostById(@PathVariable Long id, @RequestBody Post post); @DeleteExchange("/posts/{id}") - Post deletePostById(@PathVariable Long id); + void deletePostById(@PathVariable Long id); } diff --git a/httpClients/boot-http-proxy/src/main/java/com/example/rest/proxy/config/RestClientConfiguration.java b/httpClients/boot-http-proxy/src/main/java/com/example/rest/proxy/config/RestClientConfiguration.java index cf239180a..ffe392a99 100644 --- a/httpClients/boot-http-proxy/src/main/java/com/example/rest/proxy/config/RestClientConfiguration.java +++ b/httpClients/boot-http-proxy/src/main/java/com/example/rest/proxy/config/RestClientConfiguration.java @@ -2,10 +2,11 @@ import com.example.rest.proxy.client.JsonPlaceholderService; import io.micrometer.observation.ObservationRegistry; +import java.util.List; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.web.client.RestClient; import org.springframework.web.client.support.RestClientAdapter; @@ -13,6 +14,7 @@ @Configuration(proxyBeanMethods = false) @RequiredArgsConstructor +@Slf4j public class RestClientConfiguration { private final ApplicationProperties applicationProperties; @@ -21,10 +23,22 @@ public class RestClientConfiguration { HttpServiceProxyFactory httpServiceProxyFactory( RestClient.Builder builder, ObservationRegistry observationRegistry) { RestClient restClient = - builder.defaultHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE) - .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) + builder.defaultHeaders( + httpHeaders -> { + httpHeaders.setContentType(MediaType.APPLICATION_JSON); + httpHeaders.setAccept(List.of(MediaType.APPLICATION_JSON)); + }) .baseUrl(applicationProperties.getPostServiceUrl()) .observationRegistry(observationRegistry) + .requestInterceptor( + (request, body, execution) -> { + // log the http request + log.info("URI: {}", request.getURI()); + log.info("HTTP Method: {}", request.getMethod().name()); + log.info("HTTP Headers: {}", request.getHeaders()); + + return execution.execute(request, body); + }) .build(); RestClientAdapter webClientAdapter = RestClientAdapter.create(restClient); return HttpServiceProxyFactory.builderFor(webClientAdapter).build(); diff --git a/httpClients/boot-restclient/src/main/java/com/example/restclient/bootrestclient/config/RestClientConfiguration.java b/httpClients/boot-restclient/src/main/java/com/example/restclient/bootrestclient/config/RestClientConfiguration.java index 80a9e145a..be2dbf42d 100644 --- a/httpClients/boot-restclient/src/main/java/com/example/restclient/bootrestclient/config/RestClientConfiguration.java +++ b/httpClients/boot-restclient/src/main/java/com/example/restclient/bootrestclient/config/RestClientConfiguration.java @@ -2,10 +2,10 @@ import java.net.http.HttpClient; import java.time.Duration; +import java.util.List; import lombok.extern.slf4j.Slf4j; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.client.JdkClientHttpRequestFactory; import org.springframework.web.client.RestClient; @@ -20,7 +20,11 @@ RestClient restClient(RestClient.Builder builder, HttpClient jdkClient) { String baseUrl = "https://jsonplaceholder.typicode.com"; DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory(baseUrl); return builder.uriBuilderFactory(factory) - .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) + .defaultHeaders( + httpHeaders -> { + httpHeaders.setContentType(MediaType.APPLICATION_JSON); + httpHeaders.setAccept(List.of(MediaType.APPLICATION_JSON)); + }) .requestFactory(new JdkClientHttpRequestFactory(jdkClient)) .requestInterceptor( (request, body, execution) -> { diff --git a/httpClients/boot-restclient/src/test/java/com/example/restclient/bootrestclient/web/controllers/PostControllerIntTest.java b/httpClients/boot-restclient/src/test/java/com/example/restclient/bootrestclient/web/controllers/PostControllerIntTest.java index 2983e874a..b6a97ad0f 100644 --- a/httpClients/boot-restclient/src/test/java/com/example/restclient/bootrestclient/web/controllers/PostControllerIntTest.java +++ b/httpClients/boot-restclient/src/test/java/com/example/restclient/bootrestclient/web/controllers/PostControllerIntTest.java @@ -4,8 +4,13 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.Matchers.hasSize; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import com.example.restclient.bootrestclient.model.response.PostDto; import com.fasterxml.jackson.databind.ObjectMapper;