-
Notifications
You must be signed in to change notification settings - Fork 17
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
Showing
12 changed files
with
284 additions
and
10 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
dependencies { | ||
implementation 'org.springframework.boot:spring-boot-starter-web' | ||
implementation 'org.apache.httpcomponents.client5:httpclient5' | ||
testImplementation 'org.springframework.boot:spring-boot-starter-test' | ||
testImplementation 'org.junit.vintage:junit-vintage-engine' | ||
} |
31 changes: 31 additions & 0 deletions
31
spring-rest-client/src/main/java/cn/springcamp/spring/rest/client/Application.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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package cn.springcamp.spring.rest.client; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.core.ParameterizedTypeReference; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.client.RestClient; | ||
|
||
@Slf4j | ||
@SpringBootApplication | ||
@RestController | ||
public class Application { | ||
|
||
@Autowired | ||
private RestClient restClient; | ||
|
||
@GetMapping("/demo/list") | ||
public Object requestList() { | ||
return restClient.get() | ||
.uri("http://someservice/list") | ||
.retrieve() | ||
.body(new ParameterizedTypeReference<>() {}); | ||
} | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(Application.class, args); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
spring-rest-client/src/main/java/cn/springcamp/spring/rest/client/MyData.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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package cn.springcamp.spring.rest.client; | ||
|
||
public record MyData(String origin, String url) { | ||
} |
140 changes: 140 additions & 0 deletions
140
spring-rest-client/src/main/java/cn/springcamp/spring/rest/client/RestClientConfig.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 |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package cn.springcamp.spring.rest.client; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.hc.client5.http.config.ConnectionConfig; | ||
import org.apache.hc.client5.http.config.RequestConfig; | ||
import org.apache.hc.client5.http.impl.DefaultConnectionKeepAliveStrategy; | ||
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; | ||
import org.apache.hc.client5.http.impl.classic.HttpClients; | ||
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager; | ||
import org.apache.hc.client5.http.socket.ConnectionSocketFactory; | ||
import org.apache.hc.client5.http.socket.PlainConnectionSocketFactory; | ||
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory; | ||
import org.apache.hc.core5.http.config.Registry; | ||
import org.apache.hc.core5.http.config.RegistryBuilder; | ||
import org.apache.hc.core5.http.io.SocketConfig; | ||
import org.apache.hc.core5.util.TimeValue; | ||
import org.apache.hc.core5.util.Timeout; | ||
import org.springframework.boot.web.client.RestTemplateBuilder; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.HttpRequest; | ||
import org.springframework.http.HttpStatusCode; | ||
import org.springframework.http.client.ClientHttpRequestExecution; | ||
import org.springframework.http.client.ClientHttpRequestInterceptor; | ||
import org.springframework.http.client.ClientHttpResponse; | ||
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; | ||
import org.springframework.lang.NonNull; | ||
import org.springframework.util.StreamUtils; | ||
import org.springframework.web.client.RestClient; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
@Configuration | ||
public class RestClientConfig { | ||
public CloseableHttpClient httpClient() { | ||
Registry<ConnectionSocketFactory> registry = | ||
RegistryBuilder.<ConnectionSocketFactory>create() | ||
.register("http", PlainConnectionSocketFactory.getSocketFactory()) | ||
.register("https", SSLConnectionSocketFactory.getSocketFactory()) | ||
.build(); | ||
PoolingHttpClientConnectionManager poolingConnectionManager = new PoolingHttpClientConnectionManager(registry); | ||
|
||
poolingConnectionManager.setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(Timeout.ofSeconds(2)).build()); | ||
poolingConnectionManager.setDefaultConnectionConfig(ConnectionConfig.custom().setConnectTimeout(Timeout.ofSeconds(2)).build()); | ||
|
||
// set total amount of connections across all HTTP routes | ||
poolingConnectionManager.setMaxTotal(200); | ||
// set maximum amount of connections for each http route in pool | ||
poolingConnectionManager.setDefaultMaxPerRoute(200); | ||
|
||
RequestConfig requestConfig = RequestConfig.custom() | ||
.setConnectionKeepAlive(TimeValue.ofSeconds(10)) | ||
.setConnectionRequestTimeout(Timeout.ofSeconds(2)) | ||
.setResponseTimeout(Timeout.ofSeconds(2)) | ||
.build(); | ||
|
||
return HttpClients.custom() | ||
.setDefaultRequestConfig(requestConfig) | ||
.setConnectionManager(poolingConnectionManager) | ||
.setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy()) | ||
.build(); | ||
} | ||
|
||
@Slf4j | ||
static class CustomClientHttpRequestInterceptor implements ClientHttpRequestInterceptor { | ||
@Override | ||
@NonNull | ||
public ClientHttpResponse intercept(HttpRequest request, @NonNull byte[] bytes, @NonNull ClientHttpRequestExecution execution) throws IOException { | ||
log.info("HTTP Method: {}, URI: {}, Headers: {}", request.getMethod(), request.getURI(), request.getHeaders()); | ||
request.getMethod(); | ||
if (request.getMethod().equals(HttpMethod.POST)) { | ||
log.info("HTTP body: {}", new String(bytes, StandardCharsets.UTF_8)); | ||
} | ||
|
||
ClientHttpResponse response = execution.execute(request, bytes); | ||
ClientHttpResponse responseWrapper = new BufferingClientHttpResponseWrapper(response); | ||
|
||
String body = StreamUtils.copyToString(responseWrapper.getBody(), StandardCharsets.UTF_8); | ||
log.info("RESPONSE body: {}", body); | ||
|
||
return responseWrapper; | ||
} | ||
} | ||
|
||
static class BufferingClientHttpResponseWrapper implements ClientHttpResponse { | ||
|
||
private final ClientHttpResponse response; | ||
private byte[] body; | ||
|
||
BufferingClientHttpResponseWrapper(ClientHttpResponse response) { | ||
this.response = response; | ||
} | ||
|
||
@NonNull | ||
public HttpStatusCode getStatusCode() throws IOException { | ||
return this.response.getStatusCode(); | ||
} | ||
|
||
@NonNull | ||
public String getStatusText() throws IOException { | ||
return this.response.getStatusText(); | ||
} | ||
|
||
@NonNull | ||
public HttpHeaders getHeaders() { | ||
return this.response.getHeaders(); | ||
} | ||
|
||
@NonNull | ||
public InputStream getBody() throws IOException { | ||
if (this.body == null) { | ||
this.body = StreamUtils.copyToByteArray(this.response.getBody()); | ||
} | ||
return new ByteArrayInputStream(this.body); | ||
} | ||
|
||
public void close() { | ||
this.response.close(); | ||
} | ||
} | ||
|
||
@Bean | ||
public RestTemplate restTemplate(RestTemplateBuilder builder) { | ||
return builder | ||
.requestFactory(() -> new HttpComponentsClientHttpRequestFactory(httpClient())) | ||
.interceptors(new CustomClientHttpRequestInterceptor()) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public RestClient restClient(RestTemplate restTemplate) { | ||
return RestClient.builder(restTemplate).requestFactory(new HttpComponentsClientHttpRequestFactory(httpClient())).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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
92 changes: 92 additions & 0 deletions
92
spring-rest-client/src/test/java/cn/springcamp/spring/rest/client/DemoApplicationTest.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 |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package cn.springcamp.spring.rest.client; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.hamcrest.Matchers; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.web.client.TestRestTemplate; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
import org.springframework.test.web.client.MockRestServiceServer; | ||
import org.springframework.test.web.client.match.MockRestRequestMatchers; | ||
import org.springframework.web.client.RestClient; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import static org.springframework.test.web.client.ExpectedCount.manyTimes; | ||
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess; | ||
|
||
@Slf4j | ||
@RunWith(SpringRunner.class) | ||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
public class DemoApplicationTest { | ||
|
||
@Autowired | ||
private TestRestTemplate testRestTemplate; | ||
@Autowired | ||
private RestTemplate restTemplate; | ||
@Autowired | ||
private RestClient restClient; | ||
private MockRestServiceServer mockRestServiceServer; | ||
|
||
@Before | ||
public void before() { | ||
mockRestServiceServer = MockRestServiceServer.bindTo(restTemplate).ignoreExpectOrder(true).build(); | ||
|
||
this.mockRestServiceServer.expect(manyTimes(), MockRestRequestMatchers.requestTo(Matchers.startsWithIgnoringCase("http://someservice/list"))) | ||
.andRespond(withSuccess("[{\"code\": \"200\"}]", MediaType.APPLICATION_JSON)); | ||
|
||
} | ||
|
||
@Test | ||
public void testGet() { | ||
String resp = restClient.get() | ||
.uri("https://httpbin.org/get") | ||
.retrieve() | ||
.body(String.class); | ||
log.info("get: {}", resp); | ||
|
||
MyData myData = restClient.get() | ||
.uri("https://httpbin.org/get") | ||
.retrieve() | ||
.body(MyData.class); | ||
log.info("get: {}", myData); | ||
|
||
MyData postBody = new MyData("test", "test RestClient"); | ||
ResponseEntity<String> respObj = restClient.post() | ||
.uri("https://httpbin.org/post") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.body(postBody) | ||
.retrieve() | ||
.toEntity(String.class); | ||
log.info("post response: {}", respObj); | ||
|
||
// Error handling | ||
restClient.get() | ||
.uri("https://httpbin.org/status/404") | ||
.retrieve() | ||
.onStatus(status -> status.value() == 404, (request, response) -> { | ||
log.info("status 404"); | ||
}) | ||
.toBodilessEntity(); | ||
|
||
// Exchange | ||
restClient.get() | ||
.uri("https://httpbin.org/get") | ||
.accept(MediaType.APPLICATION_JSON) | ||
.exchange((request, response) -> { | ||
if (response.getStatusCode().is4xxClientError()) { | ||
log.info("status 4xx"); | ||
return ""; | ||
} else { | ||
log.info("response: {}", response); | ||
return response; | ||
} | ||
}); | ||
|
||
// testRestTemplate.getForObject("/demo/list", String.class); | ||
} | ||
} |