parent
0cd196e3dd
commit
b5b9386be6
|
@ -957,9 +957,8 @@ method parameters:
|
|||
| Dynamically set the URL for the request, overriding the annotation's `url` attribute.
|
||||
|
||||
| `UriBuilderFactory`
|
||||
| Provide a `UriBuilderFactory` to use to expand the `UriTemplate`.
|
||||
Allows dynamically setting the base URI for the request,
|
||||
while maintaining the `path` specified through annotations.
|
||||
| Provide a `UriBuilderFactory` to expand the URI template and URI variables with.
|
||||
In effect, replaces the `UriBuilderFactory` (and its base URL) of the underlying client.
|
||||
|
||||
| `HttpMethod`
|
||||
| Dynamically set the HTTP method for the request, overriding the annotation's `method` attribute
|
||||
|
|
|
@ -30,6 +30,7 @@ import org.springframework.web.client.RestClient;
|
|||
import org.springframework.web.service.invoker.HttpExchangeAdapter;
|
||||
import org.springframework.web.service.invoker.HttpRequestValues;
|
||||
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
|
||||
import org.springframework.web.util.UriBuilderFactory;
|
||||
|
||||
/**
|
||||
* {@link HttpExchangeAdapter} that enables an {@link HttpServiceProxyFactory}
|
||||
|
@ -93,19 +94,16 @@ public final class RestClientAdapter implements HttpExchangeAdapter {
|
|||
if (values.getUri() != null) {
|
||||
bodySpec = uriSpec.uri(values.getUri());
|
||||
}
|
||||
|
||||
else if (values.getUriTemplate() != null) {
|
||||
if (values.getUriBuilderFactory() != null) {
|
||||
URI expanded = values.getUriBuilderFactory()
|
||||
.expand(values.getUriTemplate(), values.getUriVariables());
|
||||
bodySpec = uriSpec.uri(expanded);
|
||||
UriBuilderFactory uriBuilderFactory = values.getUriBuilderFactory();
|
||||
if (uriBuilderFactory != null) {
|
||||
URI uri = uriBuilderFactory.expand(values.getUriTemplate(), values.getUriVariables());
|
||||
bodySpec = uriSpec.uri(uri);
|
||||
}
|
||||
|
||||
else {
|
||||
bodySpec = uriSpec.uri(values.getUriTemplate(), values.getUriVariables());
|
||||
}
|
||||
}
|
||||
|
||||
else {
|
||||
throw new IllegalStateException("Neither full URL nor URI template");
|
||||
}
|
||||
|
|
|
@ -31,6 +31,7 @@ import org.springframework.web.client.RestTemplate;
|
|||
import org.springframework.web.service.invoker.HttpExchangeAdapter;
|
||||
import org.springframework.web.service.invoker.HttpRequestValues;
|
||||
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
|
||||
import org.springframework.web.util.UriBuilderFactory;
|
||||
|
||||
/**
|
||||
* {@link HttpExchangeAdapter} that enables an {@link HttpServiceProxyFactory}
|
||||
|
@ -92,19 +93,16 @@ public final class RestTemplateAdapter implements HttpExchangeAdapter {
|
|||
if (values.getUri() != null) {
|
||||
builder = RequestEntity.method(httpMethod, values.getUri());
|
||||
}
|
||||
|
||||
else if (values.getUriTemplate() != null) {
|
||||
if (values.getUriBuilderFactory() != null) {
|
||||
URI expanded = values.getUriBuilderFactory()
|
||||
.expand(values.getUriTemplate(), values.getUriVariables());
|
||||
UriBuilderFactory uriBuilderFactory = values.getUriBuilderFactory();
|
||||
if (uriBuilderFactory != null) {
|
||||
URI expanded = uriBuilderFactory.expand(values.getUriTemplate(), values.getUriVariables());
|
||||
builder = RequestEntity.method(httpMethod, expanded);
|
||||
}
|
||||
|
||||
else {
|
||||
builder = RequestEntity.method(httpMethod, values.getUriTemplate(), values.getUriVariables());
|
||||
}
|
||||
}
|
||||
|
||||
else {
|
||||
throw new IllegalStateException("Neither full URL nor URI template");
|
||||
}
|
||||
|
|
|
@ -63,10 +63,10 @@ public class HttpRequestValues {
|
|||
private final URI uri;
|
||||
|
||||
@Nullable
|
||||
private final String uriTemplate;
|
||||
private final UriBuilderFactory uriBuilderFactory;
|
||||
|
||||
@Nullable
|
||||
private final UriBuilderFactory uriBuilderFactory;
|
||||
private final String uriTemplate;
|
||||
|
||||
private final Map<String, String> uriVariables;
|
||||
|
||||
|
@ -81,11 +81,10 @@ public class HttpRequestValues {
|
|||
|
||||
|
||||
/**
|
||||
* Construct {@link HttpRequestValues}.
|
||||
*
|
||||
* @deprecated in favour of {@link HttpRequestValues#HttpRequestValues(
|
||||
* HttpMethod, URI, String, UriBuilderFactory, Map, HttpHeaders,
|
||||
* MultiValueMap, Map, Object)} to be removed in 6.2.
|
||||
* Constructor without UriBuilderFactory.
|
||||
* @deprecated in favour of
|
||||
* {@link HttpRequestValues#HttpRequestValues(HttpMethod, URI, UriBuilderFactory, String, Map, HttpHeaders, MultiValueMap, Map, Object)}
|
||||
* to be removed in 6.2.
|
||||
*/
|
||||
@Deprecated(since = "6.1", forRemoval = true)
|
||||
protected HttpRequestValues(@Nullable HttpMethod httpMethod,
|
||||
|
@ -94,13 +93,16 @@ public class HttpRequestValues {
|
|||
HttpHeaders headers, MultiValueMap<String, String> cookies, Map<String, Object> attributes,
|
||||
@Nullable Object bodyValue) {
|
||||
|
||||
this(httpMethod, uri, uriTemplate, null, uriVariables,
|
||||
headers, cookies, attributes, bodyValue);
|
||||
this(httpMethod, uri, null, uriTemplate, uriVariables, headers, cookies, attributes, bodyValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct {@link HttpRequestValues}.
|
||||
* @since 6.1
|
||||
*/
|
||||
protected HttpRequestValues(@Nullable HttpMethod httpMethod,
|
||||
@Nullable URI uri, @Nullable String uriTemplate,
|
||||
@Nullable UriBuilderFactory uriBuilderFactory, Map<String, String> uriVariables,
|
||||
@Nullable URI uri, @Nullable UriBuilderFactory uriBuilderFactory,
|
||||
@Nullable String uriTemplate, Map<String, String> uriVariables,
|
||||
HttpHeaders headers, MultiValueMap<String, String> cookies, Map<String, Object> attributes,
|
||||
@Nullable Object bodyValue) {
|
||||
|
||||
|
@ -108,8 +110,8 @@ public class HttpRequestValues {
|
|||
|
||||
this.httpMethod = httpMethod;
|
||||
this.uri = uri;
|
||||
this.uriTemplate = uriTemplate;
|
||||
this.uriBuilderFactory = uriBuilderFactory;
|
||||
this.uriTemplate = uriTemplate;
|
||||
this.uriVariables = uriVariables;
|
||||
this.headers = headers;
|
||||
this.cookies = cookies;
|
||||
|
@ -137,6 +139,19 @@ public class HttpRequestValues {
|
|||
return this.uri;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the {@link UriBuilderFactory} to expand
|
||||
* the {@link HttpRequestValues#uriTemplate} and {@link #getUriVariables()} with.
|
||||
* <p>The {@link UriBuilderFactory} is passed into the HTTP interface method
|
||||
* in order to override the UriBuilderFactory (and its baseUrl) used by the
|
||||
* underlying client.
|
||||
* @since 6.1
|
||||
*/
|
||||
@Nullable
|
||||
public UriBuilderFactory getUriBuilderFactory() {
|
||||
return this.uriBuilderFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the URL template for the request. This comes from the values in
|
||||
* class and method {@code HttpExchange} annotations.
|
||||
|
@ -146,19 +161,6 @@ public class HttpRequestValues {
|
|||
return this.uriTemplate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the {@link UriBuilderFactory} to expand
|
||||
* the {@link HttpRequestValues#uriTemplate} with.
|
||||
* <p>This comes from a {@link UriBuilderFactory} method argument.
|
||||
* It allows you to override the {@code baseUri}, while keeping the
|
||||
* path as defined in class and method
|
||||
* {@code HttpExchange} annotations.
|
||||
*/
|
||||
@Nullable
|
||||
public UriBuilderFactory getUriBuilderFactory() {
|
||||
return this.uriBuilderFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the URL template variables, or an empty map.
|
||||
*/
|
||||
|
@ -237,10 +239,10 @@ public class HttpRequestValues {
|
|||
private URI uri;
|
||||
|
||||
@Nullable
|
||||
private String uriTemplate;
|
||||
private UriBuilderFactory uriBuilderFactory;
|
||||
|
||||
@Nullable
|
||||
private UriBuilderFactory uriBuilderFactory;
|
||||
private String uriTemplate;
|
||||
|
||||
@Nullable
|
||||
private Map<String, String> uriVars;
|
||||
|
@ -282,19 +284,20 @@ public class HttpRequestValues {
|
|||
}
|
||||
|
||||
/**
|
||||
* Set the request URL as a String template.
|
||||
* Set the {@link UriBuilderFactory} that will be used to expand the
|
||||
* {@link #getUriTemplate()}.
|
||||
* @since 6.1
|
||||
*/
|
||||
public Builder setUriTemplate(String uriTemplate) {
|
||||
this.uriTemplate = uriTemplate;
|
||||
public Builder setUriBuilderFactory(@Nullable UriBuilderFactory uriBuilderFactory) {
|
||||
this.uriBuilderFactory = uriBuilderFactory;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the {@link UriBuilderFactory} that
|
||||
* will be used to expand the URI.
|
||||
* Set the request URL as a String template.
|
||||
*/
|
||||
public Builder setUriBuilderFactory(@Nullable UriBuilderFactory uriBuilderFactory) {
|
||||
this.uriBuilderFactory = uriBuilderFactory;
|
||||
public Builder setUriTemplate(String uriTemplate) {
|
||||
this.uriTemplate = uriTemplate;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -427,8 +430,8 @@ public class HttpRequestValues {
|
|||
public HttpRequestValues build() {
|
||||
|
||||
URI uri = this.uri;
|
||||
String uriTemplate = (this.uriTemplate != null ? this.uriTemplate : "");
|
||||
UriBuilderFactory uriBuilderFactory = this.uriBuilderFactory;
|
||||
String uriTemplate = (this.uriTemplate != null ? this.uriTemplate : "");
|
||||
Map<String, String> uriVars = (this.uriVars != null ? new HashMap<>(this.uriVars) : Collections.emptyMap());
|
||||
|
||||
Object bodyValue = this.bodyValue;
|
||||
|
@ -470,8 +473,8 @@ public class HttpRequestValues {
|
|||
new HashMap<>(this.attributes) : Collections.emptyMap());
|
||||
|
||||
return createRequestValues(
|
||||
this.httpMethod, uri, uriTemplate, uriBuilderFactory,
|
||||
uriVars, headers, cookies, attributes, bodyValue);
|
||||
this.httpMethod, uri, uriBuilderFactory, uriTemplate, uriVars,
|
||||
headers, cookies, attributes, bodyValue);
|
||||
}
|
||||
|
||||
protected boolean hasParts() {
|
||||
|
@ -512,9 +515,9 @@ public class HttpRequestValues {
|
|||
|
||||
/**
|
||||
* Create {@link HttpRequestValues} from values passed to the {@link Builder}.
|
||||
* @deprecated in favour of {@link Builder#createRequestValues(
|
||||
* HttpMethod, URI, String, UriBuilderFactory, Map, HttpHeaders,
|
||||
* MultiValueMap, Map, Object)} to be removed in 6.2.
|
||||
* @deprecated in favour of
|
||||
* {@link Builder#createRequestValues(HttpMethod, URI, UriBuilderFactory, String, Map, HttpHeaders, MultiValueMap, Map, Object)}
|
||||
* to be removed in 6.2.
|
||||
*/
|
||||
@Deprecated(since = "6.1", forRemoval = true)
|
||||
protected HttpRequestValues createRequestValues(
|
||||
|
@ -524,22 +527,23 @@ public class HttpRequestValues {
|
|||
HttpHeaders headers, MultiValueMap<String, String> cookies, Map<String, Object> attributes,
|
||||
@Nullable Object bodyValue) {
|
||||
|
||||
return createRequestValues(httpMethod, uri, uriTemplate, null,
|
||||
return createRequestValues(httpMethod, uri, null, uriTemplate,
|
||||
uriVars, headers, cookies, attributes, bodyValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create {@link HttpRequestValues} from values passed to the {@link Builder}.
|
||||
* @since 6.1
|
||||
*/
|
||||
protected HttpRequestValues createRequestValues(
|
||||
@Nullable HttpMethod httpMethod,
|
||||
@Nullable URI uri, @Nullable String uriTemplate,
|
||||
@Nullable UriBuilderFactory uriBuilderFactory, Map<String, String> uriVars,
|
||||
@Nullable URI uri, @Nullable UriBuilderFactory uriBuilderFactory, @Nullable String uriTemplate,
|
||||
Map<String, String> uriVars,
|
||||
HttpHeaders headers, MultiValueMap<String, String> cookies, Map<String, Object> attributes,
|
||||
@Nullable Object bodyValue) {
|
||||
|
||||
return new HttpRequestValues(
|
||||
this.httpMethod, uri, uriTemplate, uriBuilderFactory,
|
||||
this.httpMethod, uri, uriBuilderFactory, uriTemplate,
|
||||
uriVars, headers, cookies, attributes, bodyValue);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -51,14 +51,12 @@ public final class ReactiveHttpRequestValues extends HttpRequestValues {
|
|||
|
||||
private ReactiveHttpRequestValues(
|
||||
@Nullable HttpMethod httpMethod,
|
||||
@Nullable URI uri, @Nullable String uriTemplate,
|
||||
@Nullable UriBuilderFactory uriBuilderFactory, Map<String, String> uriVariables,
|
||||
@Nullable URI uri, @Nullable UriBuilderFactory uriBuilderFactory,
|
||||
@Nullable String uriTemplate, Map<String, String> uriVars,
|
||||
HttpHeaders headers, MultiValueMap<String, String> cookies, Map<String, Object> attributes,
|
||||
@Nullable Object bodyValue, @Nullable Publisher<?> body, @Nullable ParameterizedTypeReference<?> elementType) {
|
||||
|
||||
super(httpMethod, uri, uriTemplate, uriBuilderFactory,
|
||||
uriVariables, headers, cookies, attributes, bodyValue);
|
||||
|
||||
super(httpMethod, uri, uriBuilderFactory, uriTemplate, uriVars, headers, cookies, attributes, bodyValue);
|
||||
this.body = body;
|
||||
this.bodyElementType = elementType;
|
||||
}
|
||||
|
@ -135,14 +133,14 @@ public final class ReactiveHttpRequestValues extends HttpRequestValues {
|
|||
}
|
||||
|
||||
@Override
|
||||
public Builder setUriTemplate(String uriTemplate) {
|
||||
super.setUriTemplate(uriTemplate);
|
||||
public Builder setUriBuilderFactory(@Nullable UriBuilderFactory uriBuilderFactory) {
|
||||
super.setUriBuilderFactory(uriBuilderFactory);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder setUriBuilderFactory(UriBuilderFactory uriBuilderFactory) {
|
||||
super.setUriBuilderFactory(uriBuilderFactory);
|
||||
public Builder setUriTemplate(String uriTemplate) {
|
||||
super.setUriTemplate(uriTemplate);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -271,17 +269,15 @@ public final class ReactiveHttpRequestValues extends HttpRequestValues {
|
|||
@Override
|
||||
protected ReactiveHttpRequestValues createRequestValues(
|
||||
@Nullable HttpMethod httpMethod,
|
||||
@Nullable URI uri, @Nullable String uriTemplate,
|
||||
@Nullable UriBuilderFactory uriBuilderFactory, Map<String, String> uriVars,
|
||||
@Nullable URI uri, @Nullable UriBuilderFactory uriBuilderFactory,
|
||||
@Nullable String uriTemplate, Map<String, String> uriVars,
|
||||
HttpHeaders headers, MultiValueMap<String, String> cookies, Map<String, Object> attributes,
|
||||
@Nullable Object bodyValue) {
|
||||
|
||||
return new ReactiveHttpRequestValues(
|
||||
httpMethod, uri, uriTemplate, uriBuilderFactory,
|
||||
uriVars, headers, cookies, attributes,
|
||||
bodyValue, this.body, this.bodyElementType);
|
||||
httpMethod, uri, uriBuilderFactory, uriTemplate, uriVars,
|
||||
headers, cookies, attributes, bodyValue, this.body, this.bodyElementType);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -74,6 +74,7 @@ class RestClientAdapterTests {
|
|||
|
||||
private final MockWebServer anotherServer = anotherServer();
|
||||
|
||||
|
||||
@SuppressWarnings("ConstantValue")
|
||||
@AfterEach
|
||||
void shutdown() throws IOException {
|
||||
|
@ -121,19 +122,24 @@ class RestClientAdapterTests {
|
|||
|
||||
|
||||
@ParameterizedAdapterTest
|
||||
void greeting(MockWebServer server, Service service, TestObservationRegistry observationRegistry) throws Exception {
|
||||
void greeting(
|
||||
MockWebServer server, Service service, TestObservationRegistry observationRegistry) throws Exception {
|
||||
|
||||
String response = service.getGreeting();
|
||||
|
||||
RecordedRequest request = server.takeRequest();
|
||||
assertThat(response).isEqualTo("Hello Spring!");
|
||||
assertThat(request.getMethod()).isEqualTo("GET");
|
||||
assertThat(request.getPath()).isEqualTo("/greeting");
|
||||
TestObservationRegistryAssert.assertThat(observationRegistry).hasObservationWithNameEqualTo("http.client.requests")
|
||||
TestObservationRegistryAssert.assertThat(observationRegistry)
|
||||
.hasObservationWithNameEqualTo("http.client.requests")
|
||||
.that().hasLowCardinalityKeyValue("uri", "/greeting");
|
||||
}
|
||||
|
||||
@ParameterizedAdapterTest
|
||||
void greetingById(MockWebServer server, Service service, TestObservationRegistry observationRegistry) throws Exception {
|
||||
void greetingById(
|
||||
MockWebServer server, Service service, TestObservationRegistry observationRegistry) throws Exception {
|
||||
|
||||
ResponseEntity<String> response = service.getGreetingById("456");
|
||||
|
||||
RecordedRequest request = server.takeRequest();
|
||||
|
@ -141,12 +147,15 @@ class RestClientAdapterTests {
|
|||
assertThat(response.getBody()).isEqualTo("Hello Spring!");
|
||||
assertThat(request.getMethod()).isEqualTo("GET");
|
||||
assertThat(request.getPath()).isEqualTo("/greeting/456");
|
||||
TestObservationRegistryAssert.assertThat(observationRegistry).hasObservationWithNameEqualTo("http.client.requests")
|
||||
TestObservationRegistryAssert.assertThat(observationRegistry)
|
||||
.hasObservationWithNameEqualTo("http.client.requests")
|
||||
.that().hasLowCardinalityKeyValue("uri", "/greeting/{id}");
|
||||
}
|
||||
|
||||
@ParameterizedAdapterTest
|
||||
void greetingWithDynamicUri(MockWebServer server, Service service, TestObservationRegistry observationRegistry) throws Exception {
|
||||
void greetingWithDynamicUri(
|
||||
MockWebServer server, Service service, TestObservationRegistry observationRegistry) throws Exception {
|
||||
|
||||
URI dynamicUri = server.url("/greeting/123").uri();
|
||||
Optional<String> response = service.getGreetingWithDynamicUri(dynamicUri, "456");
|
||||
|
||||
|
@ -154,7 +163,8 @@ class RestClientAdapterTests {
|
|||
assertThat(response.orElse("empty")).isEqualTo("Hello Spring!");
|
||||
assertThat(request.getMethod()).isEqualTo("GET");
|
||||
assertThat(request.getRequestUrl().uri()).isEqualTo(dynamicUri);
|
||||
TestObservationRegistryAssert.assertThat(observationRegistry).hasObservationWithNameEqualTo("http.client.requests")
|
||||
TestObservationRegistryAssert.assertThat(observationRegistry)
|
||||
.hasObservationWithNameEqualTo("http.client.requests")
|
||||
.that().hasLowCardinalityKeyValue("uri", "none");
|
||||
}
|
||||
|
||||
|
@ -218,8 +228,8 @@ class RestClientAdapterTests {
|
|||
|
||||
@ParameterizedAdapterTest
|
||||
void getWithUriBuilderFactory(MockWebServer server, Service service) throws InterruptedException {
|
||||
UriBuilderFactory factory = new DefaultUriBuilderFactory(this.anotherServer.url("/")
|
||||
.toString());
|
||||
String url = this.anotherServer.url("/").toString();
|
||||
UriBuilderFactory factory = new DefaultUriBuilderFactory(url);
|
||||
|
||||
ResponseEntity<String> actualResponse = service.getWithUriBuilderFactory(factory);
|
||||
|
||||
|
@ -233,11 +243,10 @@ class RestClientAdapterTests {
|
|||
|
||||
@ParameterizedAdapterTest
|
||||
void getWithFactoryPathVariableAndRequestParam(MockWebServer server, Service service) throws InterruptedException {
|
||||
UriBuilderFactory factory = new DefaultUriBuilderFactory(this.anotherServer.url("/")
|
||||
.toString());
|
||||
String url = this.anotherServer.url("/").toString();
|
||||
UriBuilderFactory factory = new DefaultUriBuilderFactory(url);
|
||||
|
||||
ResponseEntity<String> actualResponse = service.getWithUriBuilderFactory(factory, "123",
|
||||
"test");
|
||||
ResponseEntity<String> actualResponse = service.getWithUriBuilderFactory(factory, "123", "test");
|
||||
|
||||
RecordedRequest request = this.anotherServer.takeRequest();
|
||||
assertThat(actualResponse.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
|
@ -250,8 +259,7 @@ class RestClientAdapterTests {
|
|||
@ParameterizedAdapterTest
|
||||
void getWithIgnoredUriBuilderFactory(MockWebServer server, Service service) throws InterruptedException {
|
||||
URI dynamicUri = server.url("/greeting/123").uri();
|
||||
UriBuilderFactory factory = new DefaultUriBuilderFactory(this.anotherServer.url("/")
|
||||
.toString());
|
||||
UriBuilderFactory factory = new DefaultUriBuilderFactory(this.anotherServer.url("/").toString());
|
||||
|
||||
ResponseEntity<String> actualResponse = service.getWithIgnoredUriBuilderFactory(dynamicUri, factory);
|
||||
|
||||
|
@ -265,11 +273,11 @@ class RestClientAdapterTests {
|
|||
|
||||
|
||||
private static MockWebServer anotherServer() {
|
||||
MockWebServer anotherServer = new MockWebServer();
|
||||
MockWebServer server = new MockWebServer();
|
||||
MockResponse response = new MockResponse();
|
||||
response.setHeader("Content-Type", "text/plain").setBody("Hello Spring 2!");
|
||||
anotherServer.enqueue(response);
|
||||
return anotherServer;
|
||||
server.enqueue(response);
|
||||
return server;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -35,8 +35,8 @@ class UriBuilderFactoryArgumentResolverTests {
|
|||
private final TestExchangeAdapter client = new TestExchangeAdapter();
|
||||
|
||||
private final Service service =
|
||||
HttpServiceProxyFactory.builderFor(this.client).build()
|
||||
.createClient(Service.class);
|
||||
HttpServiceProxyFactory.builderFor(this.client).build().createClient(Service.class);
|
||||
|
||||
|
||||
@Test
|
||||
void uriBuilderFactory(){
|
||||
|
|
|
@ -33,6 +33,7 @@ import org.springframework.web.service.invoker.HttpRequestValues;
|
|||
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
|
||||
import org.springframework.web.service.invoker.ReactiveHttpRequestValues;
|
||||
import org.springframework.web.service.invoker.ReactorHttpExchangeAdapter;
|
||||
import org.springframework.web.util.UriBuilderFactory;
|
||||
|
||||
/**
|
||||
* {@link ReactorHttpExchangeAdapter} that enables an {@link HttpServiceProxyFactory}
|
||||
|
@ -111,12 +112,11 @@ public final class WebClientAdapter extends AbstractReactorHttpExchangeAdapter {
|
|||
}
|
||||
|
||||
else if (values.getUriTemplate() != null) {
|
||||
if(values.getUriBuilderFactory() != null){
|
||||
URI expanded = values.getUriBuilderFactory()
|
||||
.expand(values.getUriTemplate(), values.getUriVariables());
|
||||
bodySpec = uriSpec.uri(expanded);
|
||||
UriBuilderFactory uriBuilderFactory = values.getUriBuilderFactory();
|
||||
if(uriBuilderFactory != null){
|
||||
URI uri = uriBuilderFactory.expand(values.getUriTemplate(), values.getUriVariables());
|
||||
bodySpec = uriSpec.uri(uri);
|
||||
}
|
||||
|
||||
else {
|
||||
bodySpec = uriSpec.uri(values.getUriTemplate(), values.getUriVariables());
|
||||
}
|
||||
|
|
|
@ -172,8 +172,7 @@ public class WebClientAdapterTests {
|
|||
void uriBuilderFactory() throws Exception {
|
||||
String ignoredResponseBody = "hello";
|
||||
prepareResponse(response -> response.setResponseCode(200).setBody(ignoredResponseBody));
|
||||
UriBuilderFactory factory = new DefaultUriBuilderFactory(this.anotherServer.url("/")
|
||||
.toString());
|
||||
UriBuilderFactory factory = new DefaultUriBuilderFactory(this.anotherServer.url("/").toString());
|
||||
|
||||
String actualBody = initService().getWithUriBuilderFactory(factory);
|
||||
|
||||
|
@ -186,14 +185,12 @@ public class WebClientAdapterTests {
|
|||
void uriBuilderFactoryWithPathVariableAndRequestParam() throws Exception {
|
||||
String ignoredResponseBody = "hello";
|
||||
prepareResponse(response -> response.setResponseCode(200).setBody(ignoredResponseBody));
|
||||
UriBuilderFactory factory = new DefaultUriBuilderFactory(this.anotherServer.url("/")
|
||||
.toString());
|
||||
UriBuilderFactory factory = new DefaultUriBuilderFactory(this.anotherServer.url("/").toString());
|
||||
|
||||
String actualBody = initService().getWithUriBuilderFactory(factory, "123", "test");
|
||||
|
||||
assertThat(actualBody).isEqualTo(ANOTHER_SERVER_RESPONSE_BODY);
|
||||
assertThat(this.anotherServer.takeRequest().getPath())
|
||||
.isEqualTo("/greeting/123?param=test");
|
||||
assertThat(this.anotherServer.takeRequest().getPath()).isEqualTo("/greeting/123?param=test");
|
||||
assertThat(this.server.getRequestCount()).isEqualTo(0);
|
||||
}
|
||||
|
||||
|
@ -202,8 +199,7 @@ public class WebClientAdapterTests {
|
|||
String expectedResponseBody = "hello";
|
||||
prepareResponse(response -> response.setResponseCode(200).setBody(expectedResponseBody));
|
||||
URI dynamicUri = this.server.url("/greeting/123").uri();
|
||||
UriBuilderFactory factory = new DefaultUriBuilderFactory(this.anotherServer.url("/")
|
||||
.toString());
|
||||
UriBuilderFactory factory = new DefaultUriBuilderFactory(this.anotherServer.url("/").toString());
|
||||
|
||||
String actualBody = initService().getWithIgnoredUriBuilderFactory(dynamicUri, factory);
|
||||
|
||||
|
@ -216,8 +212,7 @@ public class WebClientAdapterTests {
|
|||
private static MockWebServer anotherServer() {
|
||||
MockWebServer anotherServer = new MockWebServer();
|
||||
MockResponse response = new MockResponse();
|
||||
response.setHeader("Content-Type", "text/plain")
|
||||
.setBody(ANOTHER_SERVER_RESPONSE_BODY);
|
||||
response.setHeader("Content-Type", "text/plain").setBody(ANOTHER_SERVER_RESPONSE_BODY);
|
||||
anotherServer.enqueue(response);
|
||||
return anotherServer;
|
||||
}
|
||||
|
|
|
@ -135,17 +135,12 @@ class KotlinWebClientHttpServiceProxyTests {
|
|||
@Throws(InterruptedException::class)
|
||||
fun getWithFactoryPathVariableAndRequestParam() {
|
||||
prepareResponse { response: MockResponse ->
|
||||
response.setHeader(
|
||||
"Content-Type",
|
||||
"text/plain"
|
||||
).setBody("Hello Spring!")
|
||||
response.setHeader("Content-Type", "text/plain").setBody("Hello Spring!")
|
||||
}
|
||||
val factory: UriBuilderFactory = DefaultUriBuilderFactory(anotherServer.url("/")
|
||||
.toString())
|
||||
val factory: UriBuilderFactory = DefaultUriBuilderFactory(anotherServer.url("/").toString())
|
||||
|
||||
val actualResponse: ResponseEntity<String> = initHttpService()
|
||||
.getWithUriBuilderFactory(factory, "123",
|
||||
"test")
|
||||
val actualResponse: ResponseEntity<String> =
|
||||
initHttpService().getWithUriBuilderFactory(factory, "123", "test")
|
||||
|
||||
val request = anotherServer.takeRequest()
|
||||
assertThat(actualResponse.statusCode).isEqualTo(HttpStatus.OK)
|
||||
|
@ -159,17 +154,13 @@ class KotlinWebClientHttpServiceProxyTests {
|
|||
@Throws(InterruptedException::class)
|
||||
fun getWithIgnoredUriBuilderFactory() {
|
||||
prepareResponse { response: MockResponse ->
|
||||
response.setHeader(
|
||||
"Content-Type",
|
||||
"text/plain"
|
||||
).setBody("Hello Spring!")
|
||||
response.setHeader("Content-Type", "text/plain").setBody("Hello Spring!")
|
||||
}
|
||||
val dynamicUri = server.url("/greeting/123").uri()
|
||||
val factory: UriBuilderFactory = DefaultUriBuilderFactory(anotherServer.url("/")
|
||||
.toString())
|
||||
val factory: UriBuilderFactory = DefaultUriBuilderFactory(anotherServer.url("/").toString())
|
||||
|
||||
val actualResponse: ResponseEntity<String> = initHttpService()
|
||||
.getWithIgnoredUriBuilderFactory(dynamicUri, factory)
|
||||
val actualResponse: ResponseEntity<String> =
|
||||
initHttpService().getWithIgnoredUriBuilderFactory(dynamicUri, factory)
|
||||
|
||||
val request = server.takeRequest()
|
||||
assertThat(actualResponse.statusCode).isEqualTo(HttpStatus.OK)
|
||||
|
@ -181,9 +172,7 @@ class KotlinWebClientHttpServiceProxyTests {
|
|||
|
||||
|
||||
private fun initHttpService(): TestHttpService {
|
||||
val webClient = WebClient.builder().baseUrl(
|
||||
server.url("/").toString()
|
||||
).build()
|
||||
val webClient = WebClient.builder().baseUrl(server.url("/").toString()).build()
|
||||
return initHttpService(webClient)
|
||||
}
|
||||
|
||||
|
@ -220,8 +209,8 @@ class KotlinWebClientHttpServiceProxyTests {
|
|||
suspend fun getGreetingSuspendingWithAttribute(@RequestAttribute myAttribute: String): String
|
||||
|
||||
@GetExchange("/greeting/{id}")
|
||||
fun getWithUriBuilderFactory(uriBuilderFactory: UriBuilderFactory?,
|
||||
@PathVariable id: String?, @RequestParam param: String?): ResponseEntity<String>
|
||||
fun getWithUriBuilderFactory(
|
||||
uriBuilderFactory: UriBuilderFactory?, @PathVariable id: String?, @RequestParam param: String?): ResponseEntity<String>
|
||||
|
||||
@GetExchange("/greeting")
|
||||
fun getWithIgnoredUriBuilderFactory(uri: URI?, uriBuilderFactory: UriBuilderFactory?): ResponseEntity<String>
|
||||
|
|
Loading…
Reference in New Issue