-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ed8267b
commit e4533ae
Showing
8 changed files
with
84 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 33 additions & 2 deletions
35
...t/src/main/java/com/example/restclient/bootrestclient/config/RestClientConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters