From e4533aec55675031ab08e9b38ec120ed1c7ddbf0 Mon Sep 17 00:00:00 2001 From: Raja Kolli Date: Sun, 10 Mar 2024 15:55:49 +0000 Subject: [PATCH] feat : implement jdkClient --- httpClients/boot-rest-template/pom.xml | 33 +++++++++++------ .../config/RestTemplateConfiguration.java | 8 +++-- httpClients/boot-restclient/pom.xml | 32 ++++++++--------- .../config/RestClientConfiguration.java | 35 +++++++++++++++++-- .../exception/MyCustomRuntimeException.java | 10 ++---- .../bootrestclient/services/PostService.java | 4 +-- .../web/controllers/PostController.java | 2 +- .../web/controllers/PostControllerTest.java | 7 ++-- 8 files changed, 84 insertions(+), 47 deletions(-) diff --git a/httpClients/boot-rest-template/pom.xml b/httpClients/boot-rest-template/pom.xml index 2ee8f75f3..400a4dfa4 100644 --- a/httpClients/boot-rest-template/pom.xml +++ b/httpClients/boot-rest-template/pom.xml @@ -5,7 +5,7 @@ org.springframework.boot spring-boot-starter-parent - 3.2.3 + 3.3.0-M2 com.example.rest.template @@ -331,14 +331,25 @@ - - - spring-milestones - Spring Milestones - https://repo.spring.io/milestone - - false - - - + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + + diff --git a/httpClients/boot-rest-template/src/main/java/com/example/rest/template/config/RestTemplateConfiguration.java b/httpClients/boot-rest-template/src/main/java/com/example/rest/template/config/RestTemplateConfiguration.java index fd1ce42fb..1766a7456 100644 --- a/httpClients/boot-rest-template/src/main/java/com/example/rest/template/config/RestTemplateConfiguration.java +++ b/httpClients/boot-rest-template/src/main/java/com/example/rest/template/config/RestTemplateConfiguration.java @@ -19,6 +19,7 @@ import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.http.MediaType; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.scheduling.TaskScheduler; import org.springframework.scheduling.annotation.EnableScheduling; @@ -73,7 +74,7 @@ ConnectionKeepAliveStrategy connectionKeepAliveStrategy() { Header element = headerIterator.next(); String param = element.getName(); String value = element.getValue(); - if (value != null && param.equalsIgnoreCase("timeout")) { + if (value != null && param.equalsIgnoreCase(HttpHeaders.TIMEOUT)) { return TimeValue.ofSeconds(Long.parseLong(value)); } } @@ -88,6 +89,9 @@ RestTemplate restTemplate( return restTemplateBuilder .setConnectTimeout(Duration.ofSeconds(60)) + .defaultHeader( + org.springframework.http.HttpHeaders.CONTENT_TYPE, + MediaType.APPLICATION_JSON_VALUE) .requestFactory(() -> new HttpComponentsClientHttpRequestFactory(httpClient)) .interceptors( ((request, body, execution) -> { @@ -110,9 +114,9 @@ Runnable idleConnectionMonitor(PoolingHttpClientConnectionManager pool) { public void run() { // only if connection pool is initialised if (pool != null) { - log.info("cleaning connection pool"); pool.closeExpired(); pool.closeIdle(TimeValue.ofSeconds(IDLE_CONNECTION_WAIT_TIME)); + log.info("Idle connection monitor: Closing expired and idle connections"); } } }; diff --git a/httpClients/boot-restclient/pom.xml b/httpClients/boot-restclient/pom.xml index f34db12d2..ac9a50d41 100644 --- a/httpClients/boot-restclient/pom.xml +++ b/httpClients/boot-restclient/pom.xml @@ -5,7 +5,7 @@ org.springframework.boot spring-boot-starter-parent - 3.2.3 + 3.3.0-M2 com.example.restclient @@ -18,21 +18,21 @@ 21 2.3.0 2.43.0 - - - - - org.springframework.boot - spring-boot-starter-actuator - - - org.springframework.boot - spring-boot-starter-web - - - org.glassfish.jaxb - jaxb-runtime - provided + + + + + org.springframework.boot + spring-boot-starter-actuator + + + org.springframework.boot + spring-boot-starter-web + + + org.glassfish.jaxb + jaxb-runtime + provided org.springframework.boot 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 ba86ff8d0..80a9e145a 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 @@ -1,14 +1,45 @@ package com.example.restclient.bootrestclient.config; +import java.net.http.HttpClient; +import java.time.Duration; +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; +import org.springframework.web.util.DefaultUriBuilderFactory; @Configuration(proxyBeanMethods = false) +@Slf4j public class RestClientConfiguration { @Bean - RestClient webClient(RestClient.Builder builder) { - return builder.baseUrl("https://jsonplaceholder.typicode.com").build(); + 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) + .requestFactory(new JdkClientHttpRequestFactory(jdkClient)) + .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(); + } + + @Bean + HttpClient jdkClient() { + return HttpClient.newBuilder() + .version(HttpClient.Version.HTTP_2) + .connectTimeout(Duration.ofSeconds(30)) + .followRedirects(HttpClient.Redirect.NORMAL) + .build(); } } diff --git a/httpClients/boot-restclient/src/main/java/com/example/restclient/bootrestclient/exception/MyCustomRuntimeException.java b/httpClients/boot-restclient/src/main/java/com/example/restclient/bootrestclient/exception/MyCustomRuntimeException.java index 653a8632b..4bf905f4c 100644 --- a/httpClients/boot-restclient/src/main/java/com/example/restclient/bootrestclient/exception/MyCustomRuntimeException.java +++ b/httpClients/boot-restclient/src/main/java/com/example/restclient/bootrestclient/exception/MyCustomRuntimeException.java @@ -1,8 +1,10 @@ package com.example.restclient.bootrestclient.exception; +import lombok.Getter; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatusCode; +@Getter public class MyCustomRuntimeException extends RuntimeException { private final HttpStatusCode statusCode; private final HttpHeaders headers; @@ -11,12 +13,4 @@ public MyCustomRuntimeException(HttpStatusCode statusCode, HttpHeaders headers) this.statusCode = statusCode; this.headers = headers; } - - public HttpStatusCode getStatusCode() { - return statusCode; - } - - public HttpHeaders getHeaders() { - return headers; - } } diff --git a/httpClients/boot-restclient/src/main/java/com/example/restclient/bootrestclient/services/PostService.java b/httpClients/boot-restclient/src/main/java/com/example/restclient/bootrestclient/services/PostService.java index 8cd345ca8..77c0238ed 100644 --- a/httpClients/boot-restclient/src/main/java/com/example/restclient/bootrestclient/services/PostService.java +++ b/httpClients/boot-restclient/src/main/java/com/example/restclient/bootrestclient/services/PostService.java @@ -71,11 +71,11 @@ public Optional updatePostById(Long id, PostDto postDto) { .body(PostDto.class)); } - public PostDto deletePostById(Long id) { + public String deletePostById(Long id) { return restClient .delete() .uri(uriBuilder -> uriBuilder.path("/posts/{postId}").build(id)) .retrieve() - .body(PostDto.class); + .body(String.class); } } diff --git a/httpClients/boot-restclient/src/main/java/com/example/restclient/bootrestclient/web/controllers/PostController.java b/httpClients/boot-restclient/src/main/java/com/example/restclient/bootrestclient/web/controllers/PostController.java index b3be25dba..f324b50ac 100644 --- a/httpClients/boot-restclient/src/main/java/com/example/restclient/bootrestclient/web/controllers/PostController.java +++ b/httpClients/boot-restclient/src/main/java/com/example/restclient/bootrestclient/web/controllers/PostController.java @@ -54,7 +54,7 @@ public ResponseEntity updatePost(@PathVariable Long id, @RequestBody Po } @DeleteMapping("/{id}") - public ResponseEntity deletePost(@PathVariable Long id) { + public ResponseEntity deletePost(@PathVariable Long id) { return postService .findPostById(id) .map(postDto -> ResponseEntity.ok(postService.deletePostById(id))) diff --git a/httpClients/boot-restclient/src/test/java/com/example/restclient/bootrestclient/web/controllers/PostControllerTest.java b/httpClients/boot-restclient/src/test/java/com/example/restclient/bootrestclient/web/controllers/PostControllerTest.java index d7b9aa1de..63bcc39fc 100644 --- a/httpClients/boot-restclient/src/test/java/com/example/restclient/bootrestclient/web/controllers/PostControllerTest.java +++ b/httpClients/boot-restclient/src/test/java/com/example/restclient/bootrestclient/web/controllers/PostControllerTest.java @@ -151,12 +151,9 @@ void shouldDeletePost() throws Exception { Long postId = 1L; PostDto post = new PostDto(1L, postId, "Some text", "First Body"); given(postService.findPostById(postId)).willReturn(Optional.of(post)); - given(postService.deletePostById(post.id())).willReturn(post); + given(postService.deletePostById(post.id())).willReturn(""); - this.mockMvc - .perform(delete("/api/posts/{id}", post.id())) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.title", is(post.title()))); + this.mockMvc.perform(delete("/api/posts/{id}", post.id())).andExpect(status().isOk()); } @Test