Add UriBuilderFactoryArgumentResolver
See gh-31413
This commit is contained in:
committed by
rstoyanchev
parent
364186e699
commit
0cd196e3dd
@@ -956,6 +956,11 @@ method parameters:
|
|||||||
| `URI`
|
| `URI`
|
||||||
| Dynamically set the URL for the request, overriding the annotation's `url` attribute.
|
| 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.
|
||||||
|
|
||||||
| `HttpMethod`
|
| `HttpMethod`
|
||||||
| Dynamically set the HTTP method for the request, overriding the annotation's `method` attribute
|
| Dynamically set the HTTP method for the request, overriding the annotation's `method` attribute
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
package org.springframework.web.client.support;
|
package org.springframework.web.client.support;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -92,9 +93,19 @@ public final class RestClientAdapter implements HttpExchangeAdapter {
|
|||||||
if (values.getUri() != null) {
|
if (values.getUri() != null) {
|
||||||
bodySpec = uriSpec.uri(values.getUri());
|
bodySpec = uriSpec.uri(values.getUri());
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (values.getUriTemplate() != null) {
|
else if (values.getUriTemplate() != null) {
|
||||||
bodySpec = uriSpec.uri(values.getUriTemplate(), values.getUriVariables());
|
if (values.getUriBuilderFactory() != null) {
|
||||||
|
URI expanded = values.getUriBuilderFactory()
|
||||||
|
.expand(values.getUriTemplate(), values.getUriVariables());
|
||||||
|
bodySpec = uriSpec.uri(expanded);
|
||||||
|
}
|
||||||
|
|
||||||
|
else {
|
||||||
|
bodySpec = uriSpec.uri(values.getUriTemplate(), values.getUriVariables());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
else {
|
else {
|
||||||
throw new IllegalStateException("Neither full URL nor URI template");
|
throw new IllegalStateException("Neither full URL nor URI template");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
package org.springframework.web.client.support;
|
package org.springframework.web.client.support;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -91,9 +92,19 @@ public final class RestTemplateAdapter implements HttpExchangeAdapter {
|
|||||||
if (values.getUri() != null) {
|
if (values.getUri() != null) {
|
||||||
builder = RequestEntity.method(httpMethod, values.getUri());
|
builder = RequestEntity.method(httpMethod, values.getUri());
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (values.getUriTemplate() != null) {
|
else if (values.getUriTemplate() != null) {
|
||||||
builder = RequestEntity.method(httpMethod, values.getUriTemplate(), values.getUriVariables());
|
if (values.getUriBuilderFactory() != null) {
|
||||||
|
URI expanded = values.getUriBuilderFactory()
|
||||||
|
.expand(values.getUriTemplate(), values.getUriVariables());
|
||||||
|
builder = RequestEntity.method(httpMethod, expanded);
|
||||||
|
}
|
||||||
|
|
||||||
|
else {
|
||||||
|
builder = RequestEntity.method(httpMethod, values.getUriTemplate(), values.getUriVariables());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
else {
|
else {
|
||||||
throw new IllegalStateException("Neither full URL nor URI template");
|
throw new IllegalStateException("Neither full URL nor URI template");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ import org.springframework.aot.hint.annotation.Reflective;
|
|||||||
import org.springframework.core.annotation.AliasFor;
|
import org.springframework.core.annotation.AliasFor;
|
||||||
import org.springframework.http.HttpEntity;
|
import org.springframework.http.HttpEntity;
|
||||||
import org.springframework.web.bind.annotation.Mapping;
|
import org.springframework.web.bind.annotation.Mapping;
|
||||||
|
import org.springframework.web.util.UriBuilderFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Annotation to declare a method on an HTTP service interface as an HTTP
|
* Annotation to declare a method on an HTTP service interface as an HTTP
|
||||||
@@ -61,6 +62,13 @@ import org.springframework.web.bind.annotation.Mapping;
|
|||||||
* <td>{@link org.springframework.web.service.invoker.UrlArgumentResolver}</td>
|
* <td>{@link org.springframework.web.service.invoker.UrlArgumentResolver}</td>
|
||||||
* </tr>
|
* </tr>
|
||||||
* <tr>
|
* <tr>
|
||||||
|
* <td>{@link UriBuilderFactory}</td>
|
||||||
|
* <td>Dynamically set the {@code base URI} for the request, overriding the
|
||||||
|
* one from the annotation's {@link #url()} attribute, while keeping the
|
||||||
|
* subsequent path segments as defined there</td>
|
||||||
|
* <td>{@link org.springframework.web.service.invoker.UriBuilderFactoryArgumentResolver}</td>
|
||||||
|
* </tr>
|
||||||
|
* <tr>
|
||||||
* <td>{@link org.springframework.http.HttpMethod HttpMethod}</td>
|
* <td>{@link org.springframework.http.HttpMethod HttpMethod}</td>
|
||||||
* <td>Dynamically set the HTTP method for the request, overriding the annotation's
|
* <td>Dynamically set the HTTP method for the request, overriding the annotation's
|
||||||
* {@link #method()} attribute</td>
|
* {@link #method()} attribute</td>
|
||||||
|
|||||||
@@ -36,6 +36,7 @@ import org.springframework.util.Assert;
|
|||||||
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
import org.springframework.util.LinkedMultiValueMap;
|
import org.springframework.util.LinkedMultiValueMap;
|
||||||
import org.springframework.util.MultiValueMap;
|
import org.springframework.util.MultiValueMap;
|
||||||
|
import org.springframework.web.util.UriBuilderFactory;
|
||||||
import org.springframework.web.util.UriComponentsBuilder;
|
import org.springframework.web.util.UriComponentsBuilder;
|
||||||
import org.springframework.web.util.UriUtils;
|
import org.springframework.web.util.UriUtils;
|
||||||
|
|
||||||
@@ -46,6 +47,7 @@ import org.springframework.web.util.UriUtils;
|
|||||||
* {@link HttpExchangeAdapter} to adapt to the underlying HTTP client.
|
* {@link HttpExchangeAdapter} to adapt to the underlying HTTP client.
|
||||||
*
|
*
|
||||||
* @author Rossen Stoyanchev
|
* @author Rossen Stoyanchev
|
||||||
|
* @author Olga Maciaszek-Sharma
|
||||||
* @since 6.0
|
* @since 6.0
|
||||||
*/
|
*/
|
||||||
public class HttpRequestValues {
|
public class HttpRequestValues {
|
||||||
@@ -63,6 +65,9 @@ public class HttpRequestValues {
|
|||||||
@Nullable
|
@Nullable
|
||||||
private final String uriTemplate;
|
private final String uriTemplate;
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private final UriBuilderFactory uriBuilderFactory;
|
||||||
|
|
||||||
private final Map<String, String> uriVariables;
|
private final Map<String, String> uriVariables;
|
||||||
|
|
||||||
private final HttpHeaders headers;
|
private final HttpHeaders headers;
|
||||||
@@ -75,8 +80,27 @@ public class HttpRequestValues {
|
|||||||
private final Object bodyValue;
|
private final Object bodyValue;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
@Deprecated(since = "6.1", forRemoval = true)
|
||||||
protected HttpRequestValues(@Nullable HttpMethod httpMethod,
|
protected HttpRequestValues(@Nullable HttpMethod httpMethod,
|
||||||
@Nullable URI uri, @Nullable String uriTemplate, Map<String, String> uriVariables,
|
@Nullable URI uri, @Nullable String uriTemplate,
|
||||||
|
Map<String, String> uriVariables,
|
||||||
|
HttpHeaders headers, MultiValueMap<String, String> cookies, Map<String, Object> attributes,
|
||||||
|
@Nullable Object bodyValue) {
|
||||||
|
|
||||||
|
this(httpMethod, uri, uriTemplate, null, uriVariables,
|
||||||
|
headers, cookies, attributes, bodyValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected HttpRequestValues(@Nullable HttpMethod httpMethod,
|
||||||
|
@Nullable URI uri, @Nullable String uriTemplate,
|
||||||
|
@Nullable UriBuilderFactory uriBuilderFactory, Map<String, String> uriVariables,
|
||||||
HttpHeaders headers, MultiValueMap<String, String> cookies, Map<String, Object> attributes,
|
HttpHeaders headers, MultiValueMap<String, String> cookies, Map<String, Object> attributes,
|
||||||
@Nullable Object bodyValue) {
|
@Nullable Object bodyValue) {
|
||||||
|
|
||||||
@@ -85,6 +109,7 @@ public class HttpRequestValues {
|
|||||||
this.httpMethod = httpMethod;
|
this.httpMethod = httpMethod;
|
||||||
this.uri = uri;
|
this.uri = uri;
|
||||||
this.uriTemplate = uriTemplate;
|
this.uriTemplate = uriTemplate;
|
||||||
|
this.uriBuilderFactory = uriBuilderFactory;
|
||||||
this.uriVariables = uriVariables;
|
this.uriVariables = uriVariables;
|
||||||
this.headers = headers;
|
this.headers = headers;
|
||||||
this.cookies = cookies;
|
this.cookies = cookies;
|
||||||
@@ -106,7 +131,6 @@ public class HttpRequestValues {
|
|||||||
* <p>Typically, this comes from a {@link URI} method argument, which provides
|
* <p>Typically, this comes from a {@link URI} method argument, which provides
|
||||||
* the caller with the option to override the {@link #getUriTemplate()
|
* the caller with the option to override the {@link #getUriTemplate()
|
||||||
* uriTemplate} from class and method {@code HttpExchange} annotations.
|
* uriTemplate} from class and method {@code HttpExchange} annotations.
|
||||||
* annotation.
|
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public URI getUri() {
|
public URI getUri() {
|
||||||
@@ -122,6 +146,19 @@ public class HttpRequestValues {
|
|||||||
return this.uriTemplate;
|
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.
|
* Return the URL template variables, or an empty map.
|
||||||
*/
|
*/
|
||||||
@@ -202,6 +239,9 @@ public class HttpRequestValues {
|
|||||||
@Nullable
|
@Nullable
|
||||||
private String uriTemplate;
|
private String uriTemplate;
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private UriBuilderFactory uriBuilderFactory;
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private Map<String, String> uriVars;
|
private Map<String, String> uriVars;
|
||||||
|
|
||||||
@@ -249,6 +289,15 @@ public class HttpRequestValues {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the {@link UriBuilderFactory} that
|
||||||
|
* will be used to expand the URI.
|
||||||
|
*/
|
||||||
|
public Builder setUriBuilderFactory(@Nullable UriBuilderFactory uriBuilderFactory) {
|
||||||
|
this.uriBuilderFactory = uriBuilderFactory;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a URI variable name-value pair.
|
* Add a URI variable name-value pair.
|
||||||
*/
|
*/
|
||||||
@@ -379,6 +428,7 @@ public class HttpRequestValues {
|
|||||||
|
|
||||||
URI uri = this.uri;
|
URI uri = this.uri;
|
||||||
String uriTemplate = (this.uriTemplate != null ? this.uriTemplate : "");
|
String uriTemplate = (this.uriTemplate != null ? this.uriTemplate : "");
|
||||||
|
UriBuilderFactory uriBuilderFactory = this.uriBuilderFactory;
|
||||||
Map<String, String> uriVars = (this.uriVars != null ? new HashMap<>(this.uriVars) : Collections.emptyMap());
|
Map<String, String> uriVars = (this.uriVars != null ? new HashMap<>(this.uriVars) : Collections.emptyMap());
|
||||||
|
|
||||||
Object bodyValue = this.bodyValue;
|
Object bodyValue = this.bodyValue;
|
||||||
@@ -420,7 +470,8 @@ public class HttpRequestValues {
|
|||||||
new HashMap<>(this.attributes) : Collections.emptyMap());
|
new HashMap<>(this.attributes) : Collections.emptyMap());
|
||||||
|
|
||||||
return createRequestValues(
|
return createRequestValues(
|
||||||
this.httpMethod, uri, uriTemplate, uriVars, headers, cookies, attributes, bodyValue);
|
this.httpMethod, uri, uriTemplate, uriBuilderFactory,
|
||||||
|
uriVars, headers, cookies, attributes, bodyValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected boolean hasParts() {
|
protected boolean hasParts() {
|
||||||
@@ -459,14 +510,37 @@ public class HttpRequestValues {
|
|||||||
return uriComponentsBuilder.build().toUriString();
|
return uriComponentsBuilder.build().toUriString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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(since = "6.1", forRemoval = true)
|
||||||
protected HttpRequestValues createRequestValues(
|
protected HttpRequestValues createRequestValues(
|
||||||
@Nullable HttpMethod httpMethod,
|
@Nullable HttpMethod httpMethod,
|
||||||
@Nullable URI uri, @Nullable String uriTemplate, Map<String, String> uriVars,
|
@Nullable URI uri, @Nullable String uriTemplate,
|
||||||
|
Map<String, String> uriVars,
|
||||||
|
HttpHeaders headers, MultiValueMap<String, String> cookies, Map<String, Object> attributes,
|
||||||
|
@Nullable Object bodyValue) {
|
||||||
|
|
||||||
|
return createRequestValues(httpMethod, uri, uriTemplate, null,
|
||||||
|
uriVars, headers, cookies, attributes, bodyValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create {@link HttpRequestValues} from values passed to the {@link Builder}.
|
||||||
|
*/
|
||||||
|
protected HttpRequestValues createRequestValues(
|
||||||
|
@Nullable HttpMethod httpMethod,
|
||||||
|
@Nullable URI uri, @Nullable String uriTemplate,
|
||||||
|
@Nullable UriBuilderFactory uriBuilderFactory, Map<String, String> uriVars,
|
||||||
HttpHeaders headers, MultiValueMap<String, String> cookies, Map<String, Object> attributes,
|
HttpHeaders headers, MultiValueMap<String, String> cookies, Map<String, Object> attributes,
|
||||||
@Nullable Object bodyValue) {
|
@Nullable Object bodyValue) {
|
||||||
|
|
||||||
return new HttpRequestValues(
|
return new HttpRequestValues(
|
||||||
this.httpMethod, uri, uriTemplate, uriVars, headers, cookies, attributes, bodyValue);
|
this.httpMethod, uri, uriTemplate, uriBuilderFactory,
|
||||||
|
uriVars, headers, cookies, attributes, bodyValue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -273,6 +273,7 @@ public final class HttpServiceProxyFactory {
|
|||||||
|
|
||||||
// Specific type
|
// Specific type
|
||||||
resolvers.add(new UrlArgumentResolver());
|
resolvers.add(new UrlArgumentResolver());
|
||||||
|
resolvers.add(new UriBuilderFactoryArgumentResolver());
|
||||||
resolvers.add(new HttpMethodArgumentResolver());
|
resolvers.add(new HttpMethodArgumentResolver());
|
||||||
|
|
||||||
return resolvers;
|
return resolvers;
|
||||||
|
|||||||
@@ -31,11 +31,13 @@ import org.springframework.http.client.MultipartBodyBuilder;
|
|||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.MultiValueMap;
|
import org.springframework.util.MultiValueMap;
|
||||||
|
import org.springframework.web.util.UriBuilderFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link HttpRequestValues} extension for use with {@link ReactorHttpExchangeAdapter}.
|
* {@link HttpRequestValues} extension for use with {@link ReactorHttpExchangeAdapter}.
|
||||||
*
|
*
|
||||||
* @author Rossen Stoyanchev
|
* @author Rossen Stoyanchev
|
||||||
|
* @author Olga Maciaszek-Sharma
|
||||||
* @since 6.1
|
* @since 6.1
|
||||||
*/
|
*/
|
||||||
public final class ReactiveHttpRequestValues extends HttpRequestValues {
|
public final class ReactiveHttpRequestValues extends HttpRequestValues {
|
||||||
@@ -49,11 +51,13 @@ public final class ReactiveHttpRequestValues extends HttpRequestValues {
|
|||||||
|
|
||||||
private ReactiveHttpRequestValues(
|
private ReactiveHttpRequestValues(
|
||||||
@Nullable HttpMethod httpMethod,
|
@Nullable HttpMethod httpMethod,
|
||||||
@Nullable URI uri, @Nullable String uriTemplate, Map<String, String> uriVariables,
|
@Nullable URI uri, @Nullable String uriTemplate,
|
||||||
|
@Nullable UriBuilderFactory uriBuilderFactory, Map<String, String> uriVariables,
|
||||||
HttpHeaders headers, MultiValueMap<String, String> cookies, Map<String, Object> attributes,
|
HttpHeaders headers, MultiValueMap<String, String> cookies, Map<String, Object> attributes,
|
||||||
@Nullable Object bodyValue, @Nullable Publisher<?> body, @Nullable ParameterizedTypeReference<?> elementType) {
|
@Nullable Object bodyValue, @Nullable Publisher<?> body, @Nullable ParameterizedTypeReference<?> elementType) {
|
||||||
|
|
||||||
super(httpMethod, uri, uriTemplate, uriVariables, headers, cookies, attributes, bodyValue);
|
super(httpMethod, uri, uriTemplate, uriBuilderFactory,
|
||||||
|
uriVariables, headers, cookies, attributes, bodyValue);
|
||||||
|
|
||||||
this.body = body;
|
this.body = body;
|
||||||
this.bodyElementType = elementType;
|
this.bodyElementType = elementType;
|
||||||
@@ -136,6 +140,12 @@ public final class ReactiveHttpRequestValues extends HttpRequestValues {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Builder setUriBuilderFactory(UriBuilderFactory uriBuilderFactory) {
|
||||||
|
super.setUriBuilderFactory(uriBuilderFactory);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Builder setUriVariable(String name, String value) {
|
public Builder setUriVariable(String name, String value) {
|
||||||
super.setUriVariable(name, value);
|
super.setUriVariable(name, value);
|
||||||
@@ -261,12 +271,14 @@ public final class ReactiveHttpRequestValues extends HttpRequestValues {
|
|||||||
@Override
|
@Override
|
||||||
protected ReactiveHttpRequestValues createRequestValues(
|
protected ReactiveHttpRequestValues createRequestValues(
|
||||||
@Nullable HttpMethod httpMethod,
|
@Nullable HttpMethod httpMethod,
|
||||||
@Nullable URI uri, @Nullable String uriTemplate, Map<String, String> uriVars,
|
@Nullable URI uri, @Nullable String uriTemplate,
|
||||||
|
@Nullable UriBuilderFactory uriBuilderFactory, Map<String, String> uriVars,
|
||||||
HttpHeaders headers, MultiValueMap<String, String> cookies, Map<String, Object> attributes,
|
HttpHeaders headers, MultiValueMap<String, String> cookies, Map<String, Object> attributes,
|
||||||
@Nullable Object bodyValue) {
|
@Nullable Object bodyValue) {
|
||||||
|
|
||||||
return new ReactiveHttpRequestValues(
|
return new ReactiveHttpRequestValues(
|
||||||
httpMethod, uri, uriTemplate, uriVars, headers, cookies, attributes,
|
httpMethod, uri, uriTemplate, uriBuilderFactory,
|
||||||
|
uriVars, headers, cookies, attributes,
|
||||||
bodyValue, this.body, this.bodyElementType);
|
bodyValue, this.body, this.bodyElementType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,55 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2002-2023 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.web.service.invoker;
|
||||||
|
|
||||||
|
import java.net.URL;
|
||||||
|
|
||||||
|
import org.springframework.core.MethodParameter;
|
||||||
|
import org.springframework.lang.Nullable;
|
||||||
|
import org.springframework.web.util.UriBuilderFactory;
|
||||||
|
import org.springframework.web.util.UriTemplate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An {@link HttpServiceArgumentResolver} that uses the provided
|
||||||
|
* {@link UriBuilderFactory} to expand the {@link UriTemplate}.
|
||||||
|
* <p>Unlike with the {@link UrlArgumentResolver},
|
||||||
|
* if the {@link UriBuilderFactoryArgumentResolver} is provided,
|
||||||
|
* it will not override the entire {@link URL}, but just the {@code baseUri}.
|
||||||
|
* <p>This allows for dynamically setting the {@code baseUri},
|
||||||
|
* while keeping the {@code path} specified through class
|
||||||
|
* and method annotations.
|
||||||
|
*
|
||||||
|
* @author Olga Maciaszek-Sharma
|
||||||
|
* @since 6.1
|
||||||
|
*/
|
||||||
|
public class UriBuilderFactoryArgumentResolver implements HttpServiceArgumentResolver {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean resolve(
|
||||||
|
@Nullable Object argument, MethodParameter parameter, HttpRequestValues.Builder requestValues) {
|
||||||
|
|
||||||
|
if (!parameter.getParameterType().equals(UriBuilderFactory.class)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (argument != null) {
|
||||||
|
requestValues.setUriBuilderFactory((UriBuilderFactory) argument);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
package org.springframework.web.client.support;
|
package org.springframework.web.client.support;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.lang.annotation.ElementType;
|
import java.lang.annotation.ElementType;
|
||||||
import java.lang.annotation.Retention;
|
import java.lang.annotation.Retention;
|
||||||
import java.lang.annotation.RetentionPolicy;
|
import java.lang.annotation.RetentionPolicy;
|
||||||
@@ -30,6 +31,7 @@ import io.micrometer.observation.tck.TestObservationRegistryAssert;
|
|||||||
import okhttp3.mockwebserver.MockResponse;
|
import okhttp3.mockwebserver.MockResponse;
|
||||||
import okhttp3.mockwebserver.MockWebServer;
|
import okhttp3.mockwebserver.MockWebServer;
|
||||||
import okhttp3.mockwebserver.RecordedRequest;
|
import okhttp3.mockwebserver.RecordedRequest;
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
import org.junit.jupiter.params.ParameterizedTest;
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
import org.junit.jupiter.params.provider.MethodSource;
|
import org.junit.jupiter.params.provider.MethodSource;
|
||||||
|
|
||||||
@@ -55,6 +57,7 @@ import org.springframework.web.service.invoker.HttpExchangeAdapter;
|
|||||||
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
|
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
|
||||||
import org.springframework.web.testfixture.servlet.MockMultipartFile;
|
import org.springframework.web.testfixture.servlet.MockMultipartFile;
|
||||||
import org.springframework.web.util.DefaultUriBuilderFactory;
|
import org.springframework.web.util.DefaultUriBuilderFactory;
|
||||||
|
import org.springframework.web.util.UriBuilderFactory;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
@@ -69,6 +72,16 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||||||
@SuppressWarnings("JUnitMalformedDeclaration")
|
@SuppressWarnings("JUnitMalformedDeclaration")
|
||||||
class RestClientAdapterTests {
|
class RestClientAdapterTests {
|
||||||
|
|
||||||
|
private final MockWebServer anotherServer = anotherServer();
|
||||||
|
|
||||||
|
@SuppressWarnings("ConstantValue")
|
||||||
|
@AfterEach
|
||||||
|
void shutdown() throws IOException {
|
||||||
|
if (this.anotherServer != null) {
|
||||||
|
this.anotherServer.shutdown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
@Target(ElementType.METHOD)
|
@Target(ElementType.METHOD)
|
||||||
@ParameterizedTest
|
@ParameterizedTest
|
||||||
@@ -203,6 +216,62 @@ class RestClientAdapterTests {
|
|||||||
assertThat(request.getHeader("Cookie")).isEqualTo("testCookie=test1; testCookie=test2");
|
assertThat(request.getHeader("Cookie")).isEqualTo("testCookie=test1; testCookie=test2");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ParameterizedAdapterTest
|
||||||
|
void getWithUriBuilderFactory(MockWebServer server, Service service) throws InterruptedException {
|
||||||
|
UriBuilderFactory factory = new DefaultUriBuilderFactory(this.anotherServer.url("/")
|
||||||
|
.toString());
|
||||||
|
|
||||||
|
ResponseEntity<String> actualResponse = service.getWithUriBuilderFactory(factory);
|
||||||
|
|
||||||
|
RecordedRequest request = this.anotherServer.takeRequest();
|
||||||
|
assertThat(actualResponse.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||||
|
assertThat(actualResponse.getBody()).isEqualTo("Hello Spring 2!");
|
||||||
|
assertThat(request.getMethod()).isEqualTo("GET");
|
||||||
|
assertThat(request.getPath()).isEqualTo("/greeting");
|
||||||
|
assertThat(server.getRequestCount()).isEqualTo(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedAdapterTest
|
||||||
|
void getWithFactoryPathVariableAndRequestParam(MockWebServer server, Service service) throws InterruptedException {
|
||||||
|
UriBuilderFactory factory = new DefaultUriBuilderFactory(this.anotherServer.url("/")
|
||||||
|
.toString());
|
||||||
|
|
||||||
|
ResponseEntity<String> actualResponse = service.getWithUriBuilderFactory(factory, "123",
|
||||||
|
"test");
|
||||||
|
|
||||||
|
RecordedRequest request = this.anotherServer.takeRequest();
|
||||||
|
assertThat(actualResponse.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||||
|
assertThat(actualResponse.getBody()).isEqualTo("Hello Spring 2!");
|
||||||
|
assertThat(request.getMethod()).isEqualTo("GET");
|
||||||
|
assertThat(request.getPath()).isEqualTo("/greeting/123?param=test");
|
||||||
|
assertThat(server.getRequestCount()).isEqualTo(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedAdapterTest
|
||||||
|
void getWithIgnoredUriBuilderFactory(MockWebServer server, Service service) throws InterruptedException {
|
||||||
|
URI dynamicUri = server.url("/greeting/123").uri();
|
||||||
|
UriBuilderFactory factory = new DefaultUriBuilderFactory(this.anotherServer.url("/")
|
||||||
|
.toString());
|
||||||
|
|
||||||
|
ResponseEntity<String> actualResponse = service.getWithIgnoredUriBuilderFactory(dynamicUri, factory);
|
||||||
|
|
||||||
|
RecordedRequest request = server.takeRequest();
|
||||||
|
assertThat(actualResponse.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||||
|
assertThat(actualResponse.getBody()).isEqualTo("Hello Spring!");
|
||||||
|
assertThat(request.getMethod()).isEqualTo("GET");
|
||||||
|
assertThat(request.getPath()).isEqualTo("/greeting/123");
|
||||||
|
assertThat(this.anotherServer.getRequestCount()).isEqualTo(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static MockWebServer anotherServer() {
|
||||||
|
MockWebServer anotherServer = new MockWebServer();
|
||||||
|
MockResponse response = new MockResponse();
|
||||||
|
response.setHeader("Content-Type", "text/plain").setBody("Hello Spring 2!");
|
||||||
|
anotherServer.enqueue(response);
|
||||||
|
return anotherServer;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private interface Service {
|
private interface Service {
|
||||||
|
|
||||||
@@ -231,6 +300,15 @@ class RestClientAdapterTests {
|
|||||||
void putWithSameNameCookies(
|
void putWithSameNameCookies(
|
||||||
@CookieValue("testCookie") String firstCookie, @CookieValue("testCookie") String secondCookie);
|
@CookieValue("testCookie") String firstCookie, @CookieValue("testCookie") String secondCookie);
|
||||||
|
|
||||||
|
@GetExchange("/greeting")
|
||||||
|
ResponseEntity<String> getWithUriBuilderFactory(UriBuilderFactory uriBuilderFactory);
|
||||||
|
|
||||||
|
@GetExchange("/greeting/{id}")
|
||||||
|
ResponseEntity<String> getWithUriBuilderFactory(UriBuilderFactory uriBuilderFactory,
|
||||||
|
@PathVariable String id, @RequestParam String param);
|
||||||
|
|
||||||
|
@GetExchange("/greeting")
|
||||||
|
ResponseEntity<String> getWithIgnoredUriBuilderFactory(URI uri, UriBuilderFactory uriBuilderFactory);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,6 +45,7 @@ class HttpRequestValuesTests {
|
|||||||
|
|
||||||
assertThat(requestValues.getUri()).isNull();
|
assertThat(requestValues.getUri()).isNull();
|
||||||
assertThat(requestValues.getUriTemplate()).isEmpty();
|
assertThat(requestValues.getUriTemplate()).isEmpty();
|
||||||
|
assertThat(requestValues.getUriBuilderFactory()).isNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ParameterizedTest
|
@ParameterizedTest
|
||||||
|
|||||||
@@ -0,0 +1,72 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2002-2023 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.web.service.invoker;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import org.springframework.lang.Nullable;
|
||||||
|
import org.springframework.web.service.annotation.GetExchange;
|
||||||
|
import org.springframework.web.util.DefaultUriBuilderFactory;
|
||||||
|
import org.springframework.web.util.UriBuilderFactory;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unit tests for {@link UriBuilderFactoryArgumentResolver}.
|
||||||
|
*
|
||||||
|
* @author Olga Maciaszek-Sharma
|
||||||
|
*/
|
||||||
|
class UriBuilderFactoryArgumentResolverTests {
|
||||||
|
|
||||||
|
private final TestExchangeAdapter client = new TestExchangeAdapter();
|
||||||
|
|
||||||
|
private final Service service =
|
||||||
|
HttpServiceProxyFactory.builderFor(this.client).build()
|
||||||
|
.createClient(Service.class);
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void uriBuilderFactory(){
|
||||||
|
UriBuilderFactory factory = new DefaultUriBuilderFactory("https://example.com");
|
||||||
|
this.service.execute(factory);
|
||||||
|
|
||||||
|
assertThat(getRequestValues().getUriBuilderFactory()).isEqualTo(factory);
|
||||||
|
assertThat(getRequestValues().getUriTemplate()).isEqualTo("/path");
|
||||||
|
assertThat(getRequestValues().getUri()).isNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void ignoreNullUriBuilderFactory(){
|
||||||
|
this.service.execute(null);
|
||||||
|
|
||||||
|
assertThat(getRequestValues().getUriBuilderFactory()).isEqualTo(null);
|
||||||
|
assertThat(getRequestValues().getUriTemplate()).isEqualTo("/path");
|
||||||
|
assertThat(getRequestValues().getUri()).isNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private HttpRequestValues getRequestValues() {
|
||||||
|
return this.client.getRequestValues();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private interface Service {
|
||||||
|
|
||||||
|
@GetExchange("/path")
|
||||||
|
void execute(@Nullable UriBuilderFactory uri);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -37,6 +37,7 @@ import org.springframework.web.service.annotation.PutExchange
|
|||||||
import org.springframework.web.service.invoker.HttpServiceProxyFactory
|
import org.springframework.web.service.invoker.HttpServiceProxyFactory
|
||||||
import org.springframework.web.testfixture.servlet.MockMultipartFile
|
import org.springframework.web.testfixture.servlet.MockMultipartFile
|
||||||
import org.springframework.web.util.DefaultUriBuilderFactory
|
import org.springframework.web.util.DefaultUriBuilderFactory
|
||||||
|
import org.springframework.web.util.UriBuilderFactory
|
||||||
import java.net.URI
|
import java.net.URI
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
@@ -52,10 +53,13 @@ class KotlinRestTemplateHttpServiceProxyTests {
|
|||||||
|
|
||||||
private lateinit var testService: TestService
|
private lateinit var testService: TestService
|
||||||
|
|
||||||
|
private lateinit var anotherServer: MockWebServer
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
fun setUp() {
|
fun setUp() {
|
||||||
server = MockWebServer()
|
server = MockWebServer()
|
||||||
prepareResponse()
|
prepareResponse()
|
||||||
|
anotherServer = anotherServer()
|
||||||
testService = initTestService()
|
testService = initTestService()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,6 +75,7 @@ class KotlinRestTemplateHttpServiceProxyTests {
|
|||||||
@AfterEach
|
@AfterEach
|
||||||
fun shutDown() {
|
fun shutDown() {
|
||||||
server.shutdown()
|
server.shutdown()
|
||||||
|
anotherServer.shutdown()
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -178,12 +183,73 @@ class KotlinRestTemplateHttpServiceProxyTests {
|
|||||||
.isEqualTo("testCookie=test1; testCookie=test2")
|
.isEqualTo("testCookie=test1; testCookie=test2")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Throws(InterruptedException::class)
|
||||||
|
fun getWithUriBuilderFactory() {
|
||||||
|
val factory: UriBuilderFactory = DefaultUriBuilderFactory(anotherServer.url("/")
|
||||||
|
.toString())
|
||||||
|
|
||||||
|
val actualResponse: ResponseEntity<String> = testService
|
||||||
|
.getWithUriBuilderFactory(factory)
|
||||||
|
|
||||||
|
val request = anotherServer.takeRequest()
|
||||||
|
assertThat(actualResponse.statusCode).isEqualTo(HttpStatus.OK)
|
||||||
|
assertThat(actualResponse.body).isEqualTo("Hello Spring 2!")
|
||||||
|
assertThat(request.method).isEqualTo("GET")
|
||||||
|
assertThat(request.path).isEqualTo("/greeting")
|
||||||
|
assertThat(server.requestCount).isEqualTo(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Throws(InterruptedException::class)
|
||||||
|
fun getWithFactoryPathVariableAndRequestParam() {
|
||||||
|
val factory: UriBuilderFactory = DefaultUriBuilderFactory(anotherServer.url("/")
|
||||||
|
.toString())
|
||||||
|
|
||||||
|
val actualResponse: ResponseEntity<String> = testService
|
||||||
|
.getWithUriBuilderFactory(factory, "123",
|
||||||
|
"test")
|
||||||
|
|
||||||
|
val request = anotherServer.takeRequest()
|
||||||
|
assertThat(actualResponse.statusCode).isEqualTo(HttpStatus.OK)
|
||||||
|
assertThat(actualResponse.body).isEqualTo("Hello Spring 2!")
|
||||||
|
assertThat(request.method).isEqualTo("GET")
|
||||||
|
assertThat(request.path).isEqualTo("/greeting/123?param=test")
|
||||||
|
assertThat(server.requestCount).isEqualTo(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Throws(InterruptedException::class)
|
||||||
|
fun getWithIgnoredUriBuilderFactory() {
|
||||||
|
val dynamicUri = server.url("/greeting/123").uri()
|
||||||
|
val factory: UriBuilderFactory = DefaultUriBuilderFactory(anotherServer.url("/")
|
||||||
|
.toString())
|
||||||
|
|
||||||
|
val actualResponse: ResponseEntity<String> = testService
|
||||||
|
.getWithIgnoredUriBuilderFactory(dynamicUri, factory)
|
||||||
|
|
||||||
|
val request = server.takeRequest()
|
||||||
|
assertThat(actualResponse.statusCode).isEqualTo(HttpStatus.OK)
|
||||||
|
assertThat(actualResponse.body).isEqualTo("Hello Spring!")
|
||||||
|
assertThat(request.method).isEqualTo("GET")
|
||||||
|
assertThat(request.path).isEqualTo("/greeting/123")
|
||||||
|
assertThat(anotherServer.requestCount).isEqualTo(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private fun prepareResponse() {
|
private fun prepareResponse() {
|
||||||
val response = MockResponse()
|
val response = MockResponse()
|
||||||
response.setHeader("Content-Type", "text/plain").setBody("Hello Spring!")
|
response.setHeader("Content-Type", "text/plain").setBody("Hello Spring!")
|
||||||
server.enqueue(response)
|
server.enqueue(response)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun anotherServer(): MockWebServer {
|
||||||
|
val anotherServer = MockWebServer()
|
||||||
|
val response = MockResponse()
|
||||||
|
response.setHeader("Content-Type", "text/plain").setBody("Hello Spring 2!")
|
||||||
|
anotherServer.enqueue(response)
|
||||||
|
return anotherServer
|
||||||
|
}
|
||||||
|
|
||||||
private interface TestService {
|
private interface TestService {
|
||||||
|
|
||||||
@@ -213,6 +279,16 @@ class KotlinRestTemplateHttpServiceProxyTests {
|
|||||||
@PutExchange
|
@PutExchange
|
||||||
fun putRequestWithSameNameCookies(@CookieValue("testCookie") firstCookie: String,
|
fun putRequestWithSameNameCookies(@CookieValue("testCookie") firstCookie: String,
|
||||||
@CookieValue("testCookie") secondCookie: String)
|
@CookieValue("testCookie") secondCookie: String)
|
||||||
|
|
||||||
|
@GetExchange("/greeting")
|
||||||
|
fun getWithUriBuilderFactory(uriBuilderFactory: UriBuilderFactory?): ResponseEntity<String>
|
||||||
|
|
||||||
|
@GetExchange("/greeting/{id}")
|
||||||
|
fun getWithUriBuilderFactory(uriBuilderFactory: UriBuilderFactory?,
|
||||||
|
@PathVariable id: String?, @RequestParam param: String?): ResponseEntity<String>
|
||||||
|
|
||||||
|
@GetExchange("/greeting")
|
||||||
|
fun getWithIgnoredUriBuilderFactory(uri: URI?, uriBuilderFactory: UriBuilderFactory?): ResponseEntity<String>
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -16,6 +16,8 @@
|
|||||||
|
|
||||||
package org.springframework.web.reactive.function.client.support;
|
package org.springframework.web.reactive.function.client.support;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
|
||||||
import org.reactivestreams.Publisher;
|
import org.reactivestreams.Publisher;
|
||||||
import reactor.core.publisher.Flux;
|
import reactor.core.publisher.Flux;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
@@ -96,32 +98,41 @@ public final class WebClientAdapter extends AbstractReactorHttpExchangeAdapter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("ReactiveStreamsUnusedPublisher")
|
@SuppressWarnings("ReactiveStreamsUnusedPublisher")
|
||||||
private WebClient.RequestBodySpec newRequest(HttpRequestValues requestValues) {
|
private WebClient.RequestBodySpec newRequest(HttpRequestValues values) {
|
||||||
|
|
||||||
HttpMethod httpMethod = requestValues.getHttpMethod();
|
HttpMethod httpMethod = values.getHttpMethod();
|
||||||
Assert.notNull(httpMethod, "HttpMethod is required");
|
Assert.notNull(httpMethod, "HttpMethod is required");
|
||||||
|
|
||||||
WebClient.RequestBodyUriSpec uriSpec = this.webClient.method(httpMethod);
|
WebClient.RequestBodyUriSpec uriSpec = this.webClient.method(httpMethod);
|
||||||
|
|
||||||
WebClient.RequestBodySpec bodySpec;
|
WebClient.RequestBodySpec bodySpec;
|
||||||
if (requestValues.getUri() != null) {
|
if (values.getUri() != null) {
|
||||||
bodySpec = uriSpec.uri(requestValues.getUri());
|
bodySpec = uriSpec.uri(values.getUri());
|
||||||
}
|
}
|
||||||
else if (requestValues.getUriTemplate() != null) {
|
|
||||||
bodySpec = uriSpec.uri(requestValues.getUriTemplate(), requestValues.getUriVariables());
|
else if (values.getUriTemplate() != null) {
|
||||||
|
if(values.getUriBuilderFactory() != null){
|
||||||
|
URI expanded = values.getUriBuilderFactory()
|
||||||
|
.expand(values.getUriTemplate(), values.getUriVariables());
|
||||||
|
bodySpec = uriSpec.uri(expanded);
|
||||||
|
}
|
||||||
|
|
||||||
|
else {
|
||||||
|
bodySpec = uriSpec.uri(values.getUriTemplate(), values.getUriVariables());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw new IllegalStateException("Neither full URL nor URI template");
|
throw new IllegalStateException("Neither full URL nor URI template");
|
||||||
}
|
}
|
||||||
|
|
||||||
bodySpec.headers(headers -> headers.putAll(requestValues.getHeaders()));
|
bodySpec.headers(headers -> headers.putAll(values.getHeaders()));
|
||||||
bodySpec.cookies(cookies -> cookies.putAll(requestValues.getCookies()));
|
bodySpec.cookies(cookies -> cookies.putAll(values.getCookies()));
|
||||||
bodySpec.attributes(attributes -> attributes.putAll(requestValues.getAttributes()));
|
bodySpec.attributes(attributes -> attributes.putAll(values.getAttributes()));
|
||||||
|
|
||||||
if (requestValues.getBodyValue() != null) {
|
if (values.getBodyValue() != null) {
|
||||||
bodySpec.bodyValue(requestValues.getBodyValue());
|
bodySpec.bodyValue(values.getBodyValue());
|
||||||
}
|
}
|
||||||
else if (requestValues instanceof ReactiveHttpRequestValues reactiveRequestValues) {
|
else if (values instanceof ReactiveHttpRequestValues reactiveRequestValues) {
|
||||||
Publisher<?> body = reactiveRequestValues.getBodyPublisher();
|
Publisher<?> body = reactiveRequestValues.getBodyPublisher();
|
||||||
if (body != null) {
|
if (body != null) {
|
||||||
ParameterizedTypeReference<?> elementType = reactiveRequestValues.getBodyPublisherElementType();
|
ParameterizedTypeReference<?> elementType = reactiveRequestValues.getBodyPublisherElementType();
|
||||||
|
|||||||
@@ -47,6 +47,8 @@ import org.springframework.web.service.annotation.GetExchange;
|
|||||||
import org.springframework.web.service.annotation.PostExchange;
|
import org.springframework.web.service.annotation.PostExchange;
|
||||||
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
|
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
|
||||||
import org.springframework.web.testfixture.servlet.MockMultipartFile;
|
import org.springframework.web.testfixture.servlet.MockMultipartFile;
|
||||||
|
import org.springframework.web.util.DefaultUriBuilderFactory;
|
||||||
|
import org.springframework.web.util.UriBuilderFactory;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
@@ -60,12 +62,17 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||||||
*/
|
*/
|
||||||
public class WebClientAdapterTests {
|
public class WebClientAdapterTests {
|
||||||
|
|
||||||
|
private static final String ANOTHER_SERVER_RESPONSE_BODY = "Hello Spring 2!";
|
||||||
|
|
||||||
private MockWebServer server;
|
private MockWebServer server;
|
||||||
|
|
||||||
|
private MockWebServer anotherServer;
|
||||||
|
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setUp() {
|
void setUp() {
|
||||||
this.server = new MockWebServer();
|
this.server = new MockWebServer();
|
||||||
|
this.anotherServer = anotherServer();
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("ConstantConditions")
|
@SuppressWarnings("ConstantConditions")
|
||||||
@@ -74,6 +81,10 @@ public class WebClientAdapterTests {
|
|||||||
if (this.server != null) {
|
if (this.server != null) {
|
||||||
this.server.shutdown();
|
this.server.shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.anotherServer != null) {
|
||||||
|
this.anotherServer.shutdown();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -157,6 +168,60 @@ public class WebClientAdapterTests {
|
|||||||
"Content-Type: text/plain;charset=UTF-8", "Content-Length: 5", "test2");
|
"Content-Type: text/plain;charset=UTF-8", "Content-Length: 5", "test2");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void uriBuilderFactory() throws Exception {
|
||||||
|
String ignoredResponseBody = "hello";
|
||||||
|
prepareResponse(response -> response.setResponseCode(200).setBody(ignoredResponseBody));
|
||||||
|
UriBuilderFactory factory = new DefaultUriBuilderFactory(this.anotherServer.url("/")
|
||||||
|
.toString());
|
||||||
|
|
||||||
|
String actualBody = initService().getWithUriBuilderFactory(factory);
|
||||||
|
|
||||||
|
assertThat(actualBody).isEqualTo(ANOTHER_SERVER_RESPONSE_BODY);
|
||||||
|
assertThat(this.anotherServer.takeRequest().getPath()).isEqualTo("/greeting");
|
||||||
|
assertThat(this.server.getRequestCount()).isEqualTo(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void uriBuilderFactoryWithPathVariableAndRequestParam() throws Exception {
|
||||||
|
String ignoredResponseBody = "hello";
|
||||||
|
prepareResponse(response -> response.setResponseCode(200).setBody(ignoredResponseBody));
|
||||||
|
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.server.getRequestCount()).isEqualTo(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void ignoredUriBuilderFactory() throws Exception {
|
||||||
|
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());
|
||||||
|
|
||||||
|
String actualBody = initService().getWithIgnoredUriBuilderFactory(dynamicUri, factory);
|
||||||
|
|
||||||
|
assertThat(actualBody).isEqualTo(expectedResponseBody);
|
||||||
|
assertThat(this.server.takeRequest().getRequestUrl().uri()).isEqualTo(dynamicUri);
|
||||||
|
assertThat(this.anotherServer.getRequestCount()).isEqualTo(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static MockWebServer anotherServer() {
|
||||||
|
MockWebServer anotherServer = new MockWebServer();
|
||||||
|
MockResponse response = new MockResponse();
|
||||||
|
response.setHeader("Content-Type", "text/plain")
|
||||||
|
.setBody(ANOTHER_SERVER_RESPONSE_BODY);
|
||||||
|
anotherServer.enqueue(response);
|
||||||
|
return anotherServer;
|
||||||
|
}
|
||||||
|
|
||||||
private Service initService() {
|
private Service initService() {
|
||||||
WebClient webClient = WebClient.builder().baseUrl(this.server.url("/").toString()).build();
|
WebClient webClient = WebClient.builder().baseUrl(this.server.url("/").toString()).build();
|
||||||
return initService(webClient);
|
return initService(webClient);
|
||||||
@@ -191,6 +256,16 @@ public class WebClientAdapterTests {
|
|||||||
@PostExchange
|
@PostExchange
|
||||||
void postMultipart(MultipartFile file, @RequestPart String anotherPart);
|
void postMultipart(MultipartFile file, @RequestPart String anotherPart);
|
||||||
|
|
||||||
|
@GetExchange("/greeting")
|
||||||
|
String getWithUriBuilderFactory(UriBuilderFactory uriBuilderFactory);
|
||||||
|
|
||||||
|
@GetExchange("/greeting/{id}")
|
||||||
|
String getWithUriBuilderFactory(UriBuilderFactory uriBuilderFactory,
|
||||||
|
@PathVariable String id, @RequestParam String param);
|
||||||
|
|
||||||
|
@GetExchange("/greeting")
|
||||||
|
String getWithIgnoredUriBuilderFactory(URI uri, UriBuilderFactory uriBuilderFactory);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,15 +22,22 @@ import org.assertj.core.api.Assertions.assertThat
|
|||||||
import org.junit.jupiter.api.AfterEach
|
import org.junit.jupiter.api.AfterEach
|
||||||
import org.junit.jupiter.api.BeforeEach
|
import org.junit.jupiter.api.BeforeEach
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
|
import org.springframework.http.HttpStatus
|
||||||
|
import org.springframework.http.ResponseEntity
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable
|
||||||
import org.springframework.web.bind.annotation.RequestAttribute
|
import org.springframework.web.bind.annotation.RequestAttribute
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam
|
||||||
import org.springframework.web.reactive.function.client.ClientRequest
|
import org.springframework.web.reactive.function.client.ClientRequest
|
||||||
import org.springframework.web.reactive.function.client.ExchangeFunction
|
import org.springframework.web.reactive.function.client.ExchangeFunction
|
||||||
import org.springframework.web.reactive.function.client.WebClient
|
import org.springframework.web.reactive.function.client.WebClient
|
||||||
import org.springframework.web.service.annotation.GetExchange
|
import org.springframework.web.service.annotation.GetExchange
|
||||||
import org.springframework.web.service.invoker.HttpServiceProxyFactory
|
import org.springframework.web.service.invoker.HttpServiceProxyFactory
|
||||||
import org.springframework.web.service.invoker.createClient
|
import org.springframework.web.service.invoker.createClient
|
||||||
|
import org.springframework.web.util.DefaultUriBuilderFactory
|
||||||
|
import org.springframework.web.util.UriBuilderFactory
|
||||||
import reactor.core.publisher.Mono
|
import reactor.core.publisher.Mono
|
||||||
import reactor.test.StepVerifier
|
import reactor.test.StepVerifier
|
||||||
|
import java.net.URI
|
||||||
import java.time.Duration
|
import java.time.Duration
|
||||||
import java.util.function.Consumer
|
import java.util.function.Consumer
|
||||||
|
|
||||||
@@ -40,20 +47,24 @@ import java.util.function.Consumer
|
|||||||
*
|
*
|
||||||
* @author DongHyeon Kim
|
* @author DongHyeon Kim
|
||||||
* @author Sebastien Deleuze
|
* @author Sebastien Deleuze
|
||||||
|
* @author Olga Maciaszek-Sharma
|
||||||
*/
|
*/
|
||||||
@Suppress("DEPRECATION")
|
|
||||||
class KotlinWebClientHttpServiceProxyTests {
|
class KotlinWebClientHttpServiceProxyTests {
|
||||||
|
|
||||||
private lateinit var server: MockWebServer
|
private lateinit var server: MockWebServer
|
||||||
|
|
||||||
|
private lateinit var anotherServer: MockWebServer
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
fun setUp() {
|
fun setUp() {
|
||||||
server = MockWebServer()
|
server = MockWebServer()
|
||||||
|
anotherServer = anotherServer()
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterEach
|
@AfterEach
|
||||||
fun shutdown() {
|
fun shutdown() {
|
||||||
server.shutdown()
|
server.shutdown()
|
||||||
|
anotherServer.shutdown()
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -120,6 +131,55 @@ class KotlinWebClientHttpServiceProxyTests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Throws(InterruptedException::class)
|
||||||
|
fun getWithFactoryPathVariableAndRequestParam() {
|
||||||
|
prepareResponse { response: MockResponse ->
|
||||||
|
response.setHeader(
|
||||||
|
"Content-Type",
|
||||||
|
"text/plain"
|
||||||
|
).setBody("Hello Spring!")
|
||||||
|
}
|
||||||
|
val factory: UriBuilderFactory = DefaultUriBuilderFactory(anotherServer.url("/")
|
||||||
|
.toString())
|
||||||
|
|
||||||
|
val actualResponse: ResponseEntity<String> = initHttpService()
|
||||||
|
.getWithUriBuilderFactory(factory, "123",
|
||||||
|
"test")
|
||||||
|
|
||||||
|
val request = anotherServer.takeRequest()
|
||||||
|
assertThat(actualResponse.statusCode).isEqualTo(HttpStatus.OK)
|
||||||
|
assertThat(actualResponse.body).isEqualTo("Hello Spring 2!")
|
||||||
|
assertThat(request.method).isEqualTo("GET")
|
||||||
|
assertThat(request.path).isEqualTo("/greeting/123?param=test")
|
||||||
|
assertThat(server.requestCount).isEqualTo(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Throws(InterruptedException::class)
|
||||||
|
fun getWithIgnoredUriBuilderFactory() {
|
||||||
|
prepareResponse { response: MockResponse ->
|
||||||
|
response.setHeader(
|
||||||
|
"Content-Type",
|
||||||
|
"text/plain"
|
||||||
|
).setBody("Hello Spring!")
|
||||||
|
}
|
||||||
|
val dynamicUri = server.url("/greeting/123").uri()
|
||||||
|
val factory: UriBuilderFactory = DefaultUriBuilderFactory(anotherServer.url("/")
|
||||||
|
.toString())
|
||||||
|
|
||||||
|
val actualResponse: ResponseEntity<String> = initHttpService()
|
||||||
|
.getWithIgnoredUriBuilderFactory(dynamicUri, factory)
|
||||||
|
|
||||||
|
val request = server.takeRequest()
|
||||||
|
assertThat(actualResponse.statusCode).isEqualTo(HttpStatus.OK)
|
||||||
|
assertThat(actualResponse.body).isEqualTo("Hello Spring!")
|
||||||
|
assertThat(request.method).isEqualTo("GET")
|
||||||
|
assertThat(request.path).isEqualTo("/greeting/123")
|
||||||
|
assertThat(anotherServer.requestCount).isEqualTo(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private fun initHttpService(): TestHttpService {
|
private fun initHttpService(): TestHttpService {
|
||||||
val webClient = WebClient.builder().baseUrl(
|
val webClient = WebClient.builder().baseUrl(
|
||||||
server.url("/").toString()
|
server.url("/").toString()
|
||||||
@@ -138,6 +198,14 @@ class KotlinWebClientHttpServiceProxyTests {
|
|||||||
server.enqueue(response)
|
server.enqueue(response)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun anotherServer(): MockWebServer {
|
||||||
|
val anotherServer = MockWebServer()
|
||||||
|
val response = MockResponse()
|
||||||
|
response.setHeader("Content-Type", "text/plain").setBody("Hello Spring 2!")
|
||||||
|
anotherServer.enqueue(response)
|
||||||
|
return anotherServer
|
||||||
|
}
|
||||||
|
|
||||||
private interface TestHttpService {
|
private interface TestHttpService {
|
||||||
@GetExchange("/greeting")
|
@GetExchange("/greeting")
|
||||||
suspend fun getGreetingSuspending(): String
|
suspend fun getGreetingSuspending(): String
|
||||||
@@ -150,5 +218,12 @@ class KotlinWebClientHttpServiceProxyTests {
|
|||||||
|
|
||||||
@GetExchange("/greeting")
|
@GetExchange("/greeting")
|
||||||
suspend fun getGreetingSuspendingWithAttribute(@RequestAttribute myAttribute: String): String
|
suspend fun getGreetingSuspendingWithAttribute(@RequestAttribute myAttribute: String): String
|
||||||
|
|
||||||
|
@GetExchange("/greeting/{id}")
|
||||||
|
fun getWithUriBuilderFactory(uriBuilderFactory: UriBuilderFactory?,
|
||||||
|
@PathVariable id: String?, @RequestParam param: String?): ResponseEntity<String>
|
||||||
|
|
||||||
|
@GetExchange("/greeting")
|
||||||
|
fun getWithIgnoredUriBuilderFactory(uri: URI?, uriBuilderFactory: UriBuilderFactory?): ResponseEntity<String>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user