Browse Source

Uses new RestClientCustomizer to add ClientHttpRequestFactory.

Changes constructor of RestClientProxyExchange to take a RestClient.
mvc-server
sgibb 1 year ago
parent
commit
92cb892b95
No known key found for this signature in database
GPG Key ID: 7788A47380690861
  1. 14
      spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/GatewayServerMvcAutoConfiguration.java
  2. 5
      spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/handler/RestClientProxyExchange.java
  3. 1
      spring-cloud-gateway-server-mvc/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports

14
spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/GatewayServerMvcAutoConfiguration.java

@ -19,7 +19,9 @@ package org.springframework.cloud.gateway.server.mvc; @@ -19,7 +19,9 @@ package org.springframework.cloud.gateway.server.mvc;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.web.client.RestClientAutoConfiguration;
import org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration;
import org.springframework.boot.web.client.RestClientCustomizer;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.gateway.server.mvc.config.GatewayMvcProperties;
import org.springframework.cloud.gateway.server.mvc.config.GatewayMvcPropertiesBeanDefinitionRegistrar;
@ -39,8 +41,9 @@ import org.springframework.context.annotation.Import; @@ -39,8 +41,9 @@ import org.springframework.context.annotation.Import;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.JdkClientHttpRequestFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestClient;
@AutoConfiguration(after = RestTemplateAutoConfiguration.class)
@AutoConfiguration(after = { RestTemplateAutoConfiguration.class, RestClientAutoConfiguration.class })
@Import(GatewayMvcPropertiesBeanDefinitionRegistrar.class)
public class GatewayServerMvcAutoConfiguration {
@ -51,11 +54,16 @@ public class GatewayServerMvcAutoConfiguration { @@ -51,11 +54,16 @@ public class GatewayServerMvcAutoConfiguration {
return new ClientHttpRequestFactoryProxyExchange(requestFactory);
}
@Bean
public RestClientCustomizer gatewayRestClientCustomizer(ClientHttpRequestFactory requestFactory) {
return restClientBuilder -> restClientBuilder.requestFactory(requestFactory);
}
// Make default when reflection is no longer needed to function
// @Bean
@ConditionalOnMissingBean(ProxyExchange.class)
public RestClientProxyExchange restClientProxyExchange(ClientHttpRequestFactory requestFactory) {
return new RestClientProxyExchange(requestFactory);
public RestClientProxyExchange restClientProxyExchange(RestClient.Builder restClientBuilder) {
return new RestClientProxyExchange(restClientBuilder.build());
}
@Bean

5
spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/handler/RestClientProxyExchange.java

@ -19,7 +19,6 @@ package org.springframework.cloud.gateway.server.mvc.handler; @@ -19,7 +19,6 @@ package org.springframework.cloud.gateway.server.mvc.handler;
import java.io.IOException;
import java.lang.reflect.Field;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
@ -33,8 +32,8 @@ public class RestClientProxyExchange implements ProxyExchange { @@ -33,8 +32,8 @@ public class RestClientProxyExchange implements ProxyExchange {
private final Field clientResponseField;
public RestClientProxyExchange(ClientHttpRequestFactory requestFactory) {
restClient = RestClient.builder().requestFactory(requestFactory).build();
public RestClientProxyExchange(RestClient restClient) {
this.restClient = restClient;
try {
clientResponseField = ReflectionUtils.findField(
ClassUtils.forName("org.springframework.web.client.DefaultRestClient$DefaultResponseSpec", null),

1
spring-cloud-gateway-server-mvc/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports

@ -1,2 +1,3 @@ @@ -1,2 +1,3 @@
org.springframework.cloud.gateway.server.mvc.GatewayServerMvcAutoConfiguration
org.springframework.cloud.gateway.server.mvc.handler.GatewayMultipartAutoConfiguration
org.springframework.boot.autoconfigure.web.client.RestClientAutoConfiguration
Loading…
Cancel
Save