Skip to content

Commit

Permalink
feat : polish configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli committed Mar 10, 2024
1 parent 3b66ecb commit 9a24ee9
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

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;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;

@Configuration(proxyBeanMethods = false)
@RequiredArgsConstructor
@Slf4j
public class RestClientConfiguration {

private final ApplicationProperties applicationProperties;
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 9a24ee9

Please sign in to comment.