Browse Source

Update after review

See gh-28386
pull/28395/head
rstoyanchev 3 years ago
parent
commit
564f8ba7a0
  1. 18
      spring-web/src/main/java/org/springframework/web/service/annotation/GetExchange.java
  2. 5
      spring-web/src/main/java/org/springframework/web/service/annotation/HttpExchange.java
  3. 23
      spring-web/src/main/java/org/springframework/web/service/annotation/PostExchange.java
  4. 19
      spring-web/src/main/java/org/springframework/web/service/annotation/PutExchange.java
  5. 18
      spring-web/src/main/java/org/springframework/web/service/invoker/HttpClientAdapter.java
  6. 8
      spring-web/src/main/java/org/springframework/web/service/invoker/HttpMethodArgumentResolver.java
  7. 17
      spring-web/src/main/java/org/springframework/web/service/invoker/HttpRequestSpec.java
  8. 10
      spring-web/src/main/java/org/springframework/web/service/invoker/HttpServiceArgumentResolver.java
  9. 80
      spring-web/src/main/java/org/springframework/web/service/invoker/HttpServiceMethod.java
  10. 20
      spring-web/src/main/java/org/springframework/web/service/invoker/HttpServiceProxyFactory.java
  11. 14
      spring-web/src/main/java/org/springframework/web/service/invoker/PathVariableArgumentResolver.java
  12. 2
      spring-web/src/main/java/org/springframework/web/service/invoker/package-info.java
  13. 16
      spring-web/src/test/java/org/springframework/web/service/invoker/HttpMethodArgumentResolverTests.java
  14. 58
      spring-web/src/test/java/org/springframework/web/service/invoker/HttpServiceMethodTests.java
  15. 26
      spring-web/src/test/java/org/springframework/web/service/invoker/PathVariableArgumentResolverTests.java
  16. 44
      spring-web/src/test/java/org/springframework/web/service/invoker/TestHttpClientAdapter.java
  17. 58
      spring-webflux/src/main/java/org/springframework/web/reactive/function/client/support/WebClientAdapter.java
  18. 6
      spring-webflux/src/test/java/org/springframework/web/reactive/function/client/support/WebClientHttpServiceProxyTests.java

18
spring-web/src/main/java/org/springframework/web/service/annotation/GetRequest.java → spring-web/src/main/java/org/springframework/web/service/annotation/GetExchange.java

@ -26,7 +26,7 @@ import org.springframework.core.annotation.AliasFor; @@ -26,7 +26,7 @@ import org.springframework.core.annotation.AliasFor;
/**
* Shortcut for {@link HttpRequest} for HTTP GET requests.
* Shortcut for {@link HttpExchange} for HTTP GET requests.
*
* @author Rossen Stoyanchev
* @since 6.0
@ -34,25 +34,25 @@ import org.springframework.core.annotation.AliasFor; @@ -34,25 +34,25 @@ import org.springframework.core.annotation.AliasFor;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@HttpRequest(method = "GET")
public @interface GetRequest {
@HttpExchange(method = "GET")
public @interface GetExchange {
/**
* Alias for {@link HttpRequest#value}.
* Alias for {@link HttpExchange#value}.
*/
@AliasFor(annotation = HttpRequest.class)
@AliasFor(annotation = HttpExchange.class)
String value() default "";
/**
* Alias for {@link HttpRequest#url()}.
* Alias for {@link HttpExchange#url()}.
*/
@AliasFor(annotation = HttpRequest.class)
@AliasFor(annotation = HttpExchange.class)
String url() default "";
/**
* Alias for {@link HttpRequest#accept()}.
* Alias for {@link HttpExchange#accept()}.
*/
@AliasFor(annotation = HttpRequest.class)
@AliasFor(annotation = HttpExchange.class)
String[] accept() default {};
}

5
spring-web/src/main/java/org/springframework/web/service/annotation/HttpRequest.java → spring-web/src/main/java/org/springframework/web/service/annotation/HttpExchange.java

@ -42,7 +42,7 @@ import org.springframework.web.bind.annotation.Mapping; @@ -42,7 +42,7 @@ import org.springframework.web.bind.annotation.Mapping;
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface HttpRequest {
public @interface HttpExchange {
/**
* This is an alias for {@link #url}.
@ -52,7 +52,7 @@ public @interface HttpRequest { @@ -52,7 +52,7 @@ public @interface HttpRequest {
/**
* The URL for the request, either a full URL or a path only that is relative
* to a URL declared in a type-level {@code @HttpRequest}, and/or a globally
* to a URL declared in a type-level {@code @HttpExchange}, and/or a globally
* configured base URL.
* <p>By default, this is empty.
*/
@ -67,7 +67,6 @@ public @interface HttpRequest { @@ -67,7 +67,6 @@ public @interface HttpRequest {
*/
String method() default "";
/**
* The media type for the {@code "Content-Type"} header.
* <p>Supported at the type level as well as at the method level, in which

23
spring-web/src/main/java/org/springframework/web/service/annotation/PostRequest.java → spring-web/src/main/java/org/springframework/web/service/annotation/PostExchange.java

@ -24,8 +24,9 @@ import java.lang.annotation.Target; @@ -24,8 +24,9 @@ import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;
/**
* Shortcut for {@link HttpRequest} for HTTP POST requests.
* Shortcut for {@link HttpExchange} for HTTP POST requests.
*
* @author Rossen Stoyanchev
* @since 6.0
@ -33,31 +34,31 @@ import org.springframework.core.annotation.AliasFor; @@ -33,31 +34,31 @@ import org.springframework.core.annotation.AliasFor;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@HttpRequest(method = "POST")
public @interface PostRequest {
@HttpExchange(method = "POST")
public @interface PostExchange {
/**
* Alias for {@link HttpRequest#value}.
* Alias for {@link HttpExchange#value}.
*/
@AliasFor(annotation = HttpRequest.class)
@AliasFor(annotation = HttpExchange.class)
String value() default "";
/**
* Alias for {@link HttpRequest#url()}.
* Alias for {@link HttpExchange#url()}.
*/
@AliasFor(annotation = HttpRequest.class)
@AliasFor(annotation = HttpExchange.class)
String url() default "";
/**
* Alias for {@link HttpRequest#contentType()}.
* Alias for {@link HttpExchange#contentType()}.
*/
@AliasFor(annotation = HttpRequest.class)
@AliasFor(annotation = HttpExchange.class)
String contentType() default "";
/**
* Alias for {@link HttpRequest#accept()}.
* Alias for {@link HttpExchange#accept()}.
*/
@AliasFor(annotation = HttpRequest.class)
@AliasFor(annotation = HttpExchange.class)
String[] accept() default {};
}

19
spring-web/src/main/java/org/springframework/web/service/annotation/PutRequest.java → spring-web/src/main/java/org/springframework/web/service/annotation/PutExchange.java

@ -24,8 +24,9 @@ import java.lang.annotation.Target; @@ -24,8 +24,9 @@ import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;
/**
* Shortcut for {@link HttpRequest} for HTTP PUT requests.
* Shortcut for {@link HttpExchange} for HTTP PUT requests.
*
* @author Rossen Stoyanchev
* @since 6.0
@ -33,25 +34,25 @@ import org.springframework.core.annotation.AliasFor; @@ -33,25 +34,25 @@ import org.springframework.core.annotation.AliasFor;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@HttpRequest(method = "PUT")
public @interface PutRequest {
@HttpExchange(method = "PUT")
public @interface PutExchange {
/**
* Alias for {@link HttpRequest#value}.
* Alias for {@link HttpExchange#value}.
*/
@AliasFor(annotation = HttpRequest.class)
@AliasFor(annotation = HttpExchange.class)
String[] value() default {};
/**
* Alias for {@link HttpRequest#url()}.
* Alias for {@link HttpExchange#url()}.
*/
@AliasFor(annotation = HttpRequest.class)
@AliasFor(annotation = HttpExchange.class)
String[] url() default {};
/**
* Alias for {@link HttpRequest#contentType()}.
* Alias for {@link HttpExchange#contentType()}.
*/
@AliasFor(annotation = HttpRequest.class)
@AliasFor(annotation = HttpExchange.class)
String contentType() default "";
}

18
spring-web/src/main/java/org/springframework/web/service/invoker/HttpClientAdapter.java

@ -25,26 +25,26 @@ import org.springframework.http.ResponseEntity; @@ -25,26 +25,26 @@ import org.springframework.http.ResponseEntity;
/**
* Decouple an {@link HttpServiceProxyFactory#createService(Class) HTTP Service proxy}
* from the underlying HTTP client.
* Contract to abstract the underlying HTTP client and decouple it from the
* {@link HttpServiceProxyFactory#createClient(Class) HTTP Service proxy}.
*
* @author Rossen Stoyanchev
* @since 6.0
*/
public interface HttpClientAdapter {
Mono<Void> requestToVoid(HttpRequestDefinition requestDefinition);
Mono<Void> requestToVoid(HttpRequestSpec spec);
Mono<HttpHeaders> requestToHeaders(HttpRequestDefinition requestDefinition);
Mono<HttpHeaders> requestToHeaders(HttpRequestSpec spec);
<T> Mono<T> requestToBody(HttpRequestDefinition requestDefinition, ParameterizedTypeReference<T> bodyType);
<T> Mono<T> requestToBody(HttpRequestSpec spec, ParameterizedTypeReference<T> bodyType);
<T> Flux<T> requestToBodyFlux(HttpRequestDefinition requestDefinition, ParameterizedTypeReference<T> bodyType);
<T> Flux<T> requestToBodyFlux(HttpRequestSpec spec, ParameterizedTypeReference<T> bodyType);
Mono<ResponseEntity<Void>> requestToBodilessEntity(HttpRequestDefinition requestDefinition);
Mono<ResponseEntity<Void>> requestToBodilessEntity(HttpRequestSpec spec);
<T> Mono<ResponseEntity<T>> requestToEntity(HttpRequestDefinition requestDefinition, ParameterizedTypeReference<T> bodyType);
<T> Mono<ResponseEntity<T>> requestToEntity(HttpRequestSpec spec, ParameterizedTypeReference<T> bodyType);
<T> Mono<ResponseEntity<Flux<T>>> requestToEntityFlux(HttpRequestDefinition requestDefinition, ParameterizedTypeReference<T> bodyType);
<T> Mono<ResponseEntity<Flux<T>>> requestToEntityFlux(HttpRequestSpec spec, ParameterizedTypeReference<T> bodyType);
}

8
spring-web/src/main/java/org/springframework/web/service/invoker/HttpMethodArgumentResolver.java

@ -25,20 +25,20 @@ import org.springframework.lang.Nullable; @@ -25,20 +25,20 @@ import org.springframework.lang.Nullable;
/**
* {@link HttpServiceMethodArgumentResolver} that resolves the target
* {@link HttpServiceArgumentResolver} that resolves the target
* request's HTTP method from an {@link HttpMethod} argument.
*
* @author Olga Maciaszek-Sharma
* @since 6.0
*/
public class HttpMethodArgumentResolver implements HttpServiceMethodArgumentResolver {
public class HttpMethodArgumentResolver implements HttpServiceArgumentResolver {
private static final Log logger = LogFactory.getLog(HttpMethodArgumentResolver.class);
@Override
public void resolve(
@Nullable Object argument, MethodParameter parameter, HttpRequestDefinition requestDefinition) {
@Nullable Object argument, MethodParameter parameter, HttpRequestSpec requestSpec) {
if (argument == null) {
return;
@ -47,7 +47,7 @@ public class HttpMethodArgumentResolver implements HttpServiceMethodArgumentReso @@ -47,7 +47,7 @@ public class HttpMethodArgumentResolver implements HttpServiceMethodArgumentReso
if (logger.isTraceEnabled()) {
logger.trace("Resolved HTTP method to: " + httpMethod.name());
}
requestDefinition.setHttpMethod(httpMethod);
requestSpec.setHttpMethod(httpMethod);
}
}

17
spring-web/src/main/java/org/springframework/web/service/invoker/HttpRequestDefinition.java → spring-web/src/main/java/org/springframework/web/service/invoker/HttpRequestSpec.java

@ -30,7 +30,6 @@ import org.springframework.core.MethodParameter; @@ -30,7 +30,6 @@ import org.springframework.core.MethodParameter;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpRequest;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
@ -39,15 +38,15 @@ import org.springframework.util.MultiValueMap; @@ -39,15 +38,15 @@ import org.springframework.util.MultiValueMap;
/**
* Container for HTTP request values accumulated from an
* {@link HttpRequest @HttpRequest}-annotated method and arguments passed to it.
* This allows an {@link HttpClientAdapter} adapt these inputs as it sees fit
* to the API of the underlying client.
* Container for HTTP request values extracted from an
* {@link org.springframework.web.service.annotation.HttpExchange @HttpExchange}-annotated
* method and argument values passed to it. This is then given to
* {@link HttpClientAdapter} to adapt to the underlying HTTP client.
*
* @author Rossen Stoyanchev
* @since 6.0
*/
public class HttpRequestDefinition {
public class HttpRequestSpec {
private static final MultiValueMap<String, String> EMPTY_COOKIES_MAP =
CollectionUtils.toMultiValueMap(Collections.emptyMap());
@ -86,6 +85,10 @@ public class HttpRequestDefinition { @@ -86,6 +85,10 @@ public class HttpRequestDefinition {
private boolean complete;
public HttpRequestSpec() {
}
public void setUri(URI uri) {
checkComplete();
this.uri = uri;
@ -175,6 +178,8 @@ public class HttpRequestDefinition { @@ -175,6 +178,8 @@ public class HttpRequestDefinition {
void setComplete() {
this.complete = true;
this.uriVariables = (this.uriVariables != null ?
Collections.unmodifiableMap(this.uriVariables) : Collections.emptyMap());

10
spring-web/src/main/java/org/springframework/web/service/invoker/HttpServiceMethodArgumentResolver.java → spring-web/src/main/java/org/springframework/web/service/invoker/HttpServiceArgumentResolver.java

@ -18,24 +18,24 @@ package org.springframework.web.service.invoker; @@ -18,24 +18,24 @@ package org.springframework.web.service.invoker;
import org.springframework.core.MethodParameter;
import org.springframework.lang.Nullable;
import org.springframework.web.service.annotation.HttpRequest;
import org.springframework.web.service.annotation.HttpExchange;
/**
* Resolve an argument from an {@link HttpRequest @HttpRequest} annotated method
* Resolve an argument from an {@link HttpExchange @HttpExchange}-annotated method
* to one or more HTTP request values.
*
* @author Rossen Stoyanchev
* @since 6.0
*/
public interface HttpServiceMethodArgumentResolver {
public interface HttpServiceArgumentResolver {
/**
* Resolve the argument value.
* @param argument the argument value
* @param parameter the method parameter for the argument
* @param requestDefinition container to add HTTP request values to
* @param requestSpec container to add HTTP request values to
*/
void resolve(@Nullable Object argument, MethodParameter parameter, HttpRequestDefinition requestDefinition);
void resolve(@Nullable Object argument, MethodParameter parameter, HttpRequestSpec requestSpec);
}

80
spring-web/src/main/java/org/springframework/web/service/invoker/HttpServiceMethod.java

@ -43,12 +43,12 @@ import org.springframework.lang.Nullable; @@ -43,12 +43,12 @@ import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.service.annotation.HttpRequest;
import org.springframework.web.service.annotation.HttpExchange;
/**
* Implements the invocation of an {@link HttpRequest @HttpRequest} annotated,
* {@link HttpServiceProxyFactory#createService(Class) HTTP Service proxy} method
* Implements the invocation of an {@link HttpExchange @HttpExchange}-annotated,
* {@link HttpServiceProxyFactory#createClient(Class) HTTP Service proxy} method
* by delegating to an {@link HttpClientAdapter} to perform actual requests.
*
* @author Rossen Stoyanchev
@ -60,22 +60,22 @@ final class HttpServiceMethod { @@ -60,22 +60,22 @@ final class HttpServiceMethod {
private final MethodParameter[] parameters;
private final List<HttpServiceMethodArgumentResolver> argumentResolvers;
private final List<HttpServiceArgumentResolver> argumentResolvers;
private final HttpRequestDefinitionFactory requestDefinitionFactory;
private final HttpRequestSpecFactory requestSpecFactory;
private final ResponseFunction responseFunction;
HttpServiceMethod(
Method method, Class<?> containingClass, List<HttpServiceMethodArgumentResolver> argumentResolvers,
Method method, Class<?> containingClass, List<HttpServiceArgumentResolver> argumentResolvers,
HttpClientAdapter client, ReactiveAdapterRegistry reactiveRegistry,
Duration blockTimeout) {
this.method = method;
this.parameters = initMethodParameters(method);
this.argumentResolvers = argumentResolvers;
this.requestDefinitionFactory = HttpRequestDefinitionFactory.create(method, containingClass);
this.requestSpecFactory = HttpRequestSpecFactory.create(method, containingClass);
this.responseFunction = ResponseFunction.create(client, method, reactiveRegistry, blockTimeout);
}
@ -96,34 +96,34 @@ final class HttpServiceMethod { @@ -96,34 +96,34 @@ final class HttpServiceMethod {
@Nullable
public Object invoke(Object[] arguments) {
HttpRequestDefinition requestDefinition = this.requestDefinitionFactory.initializeRequest();
applyArguments(requestDefinition, arguments);
requestDefinition.setComplete();
return this.responseFunction.execute(requestDefinition);
HttpRequestSpec requestSpec = this.requestSpecFactory.initializeRequestSpec();
applyArguments(requestSpec, arguments);
requestSpec.setComplete();
return this.responseFunction.execute(requestSpec);
}
private void applyArguments(HttpRequestDefinition requestDefinition, Object[] arguments) {
private void applyArguments(HttpRequestSpec requestSpec, Object[] arguments) {
Assert.isTrue(arguments.length == this.parameters.length, "Method argument mismatch");
for (int i = 0; i < this.parameters.length; i++) {
Object argumentValue = arguments[i];
ParameterNameDiscoverer nameDiscoverer = new DefaultParameterNameDiscoverer();
this.parameters[i].initParameterNameDiscovery(nameDiscoverer);
for (HttpServiceMethodArgumentResolver resolver : this.argumentResolvers) {
resolver.resolve(argumentValue, this.parameters[i], requestDefinition);
for (HttpServiceArgumentResolver resolver : this.argumentResolvers) {
resolver.resolve(argumentValue, this.parameters[i], requestSpec);
}
}
}
/**
* Factory for an {@link HttpRequestDefinition} with values extracted from
* the type and method-level {@link HttpRequest @HttpRequest} annotations.
* Factory for an {@link HttpRequestSpec} with values extracted from
* the type and method-level {@link HttpExchange @HttpRequest} annotations.
*/
private record HttpRequestDefinitionFactory(
private record HttpRequestSpecFactory(
@Nullable HttpMethod httpMethod, @Nullable String url,
@Nullable MediaType contentType, @Nullable List<MediaType> acceptMediaTypes) {
private HttpRequestDefinitionFactory(
private HttpRequestSpecFactory(
@Nullable HttpMethod httpMethod, @Nullable String url,
@Nullable MediaType contentType, @Nullable List<MediaType> acceptMediaTypes) {
@ -133,31 +133,31 @@ final class HttpServiceMethod { @@ -133,31 +133,31 @@ final class HttpServiceMethod {
this.acceptMediaTypes = acceptMediaTypes;
}
public HttpRequestDefinition initializeRequest() {
HttpRequestDefinition requestDefinition = new HttpRequestDefinition();
public HttpRequestSpec initializeRequestSpec() {
HttpRequestSpec requestSpec = new HttpRequestSpec();
if (this.httpMethod != null) {
requestDefinition.setHttpMethod(this.httpMethod);
requestSpec.setHttpMethod(this.httpMethod);
}
if (this.url != null) {
requestDefinition.setUriTemplate(this.url);
requestSpec.setUriTemplate(this.url);
}
if (this.contentType != null) {
requestDefinition.getHeaders().setContentType(this.contentType);
requestSpec.getHeaders().setContentType(this.contentType);
}
if (this.acceptMediaTypes != null) {
requestDefinition.getHeaders().setAccept(this.acceptMediaTypes);
requestSpec.getHeaders().setAccept(this.acceptMediaTypes);
}
return requestDefinition;
return requestSpec;
}
/**
* Introspect the method and create the request factory for it.
*/
public static HttpRequestDefinitionFactory create(Method method, Class<?> containingClass) {
public static HttpRequestSpecFactory create(Method method, Class<?> containingClass) {
HttpRequest annot1 = AnnotatedElementUtils.findMergedAnnotation(containingClass, HttpRequest.class);
HttpRequest annot2 = AnnotatedElementUtils.findMergedAnnotation(method, HttpRequest.class);
HttpExchange annot1 = AnnotatedElementUtils.findMergedAnnotation(containingClass, HttpExchange.class);
HttpExchange annot2 = AnnotatedElementUtils.findMergedAnnotation(method, HttpExchange.class);
Assert.notNull(annot2, "Expected HttpRequest annotation");
@ -166,12 +166,12 @@ final class HttpServiceMethod { @@ -166,12 +166,12 @@ final class HttpServiceMethod {
MediaType contentType = initContentType(annot1, annot2);
List<MediaType> acceptableMediaTypes = initAccept(annot1, annot2);
return new HttpRequestDefinitionFactory(httpMethod, url, contentType, acceptableMediaTypes);
return new HttpRequestSpecFactory(httpMethod, url, contentType, acceptableMediaTypes);
}
@Nullable
private static HttpMethod initHttpMethod(@Nullable HttpRequest typeAnnot, HttpRequest annot) {
private static HttpMethod initHttpMethod(@Nullable HttpExchange typeAnnot, HttpExchange annot) {
String value1 = (typeAnnot != null ? typeAnnot.method() : null);
String value2 = annot.method();
@ -188,7 +188,7 @@ final class HttpServiceMethod { @@ -188,7 +188,7 @@ final class HttpServiceMethod {
}
@Nullable
private static String initUrl(@Nullable HttpRequest typeAnnot, HttpRequest annot) {
private static String initUrl(@Nullable HttpExchange typeAnnot, HttpExchange annot) {
String url1 = (typeAnnot != null ? typeAnnot.url() : null);
String url2 = annot.url();
@ -208,7 +208,7 @@ final class HttpServiceMethod { @@ -208,7 +208,7 @@ final class HttpServiceMethod {
}
@Nullable
private static MediaType initContentType(@Nullable HttpRequest typeAnnot, HttpRequest annot) {
private static MediaType initContentType(@Nullable HttpExchange typeAnnot, HttpExchange annot) {
String value1 = (typeAnnot != null ? typeAnnot.contentType() : null);
String value2 = annot.contentType();
@ -225,7 +225,7 @@ final class HttpServiceMethod { @@ -225,7 +225,7 @@ final class HttpServiceMethod {
}
@Nullable
private static List<MediaType> initAccept(@Nullable HttpRequest typeAnnot, HttpRequest annot) {
private static List<MediaType> initAccept(@Nullable HttpExchange typeAnnot, HttpExchange annot) {
String[] value1 = (typeAnnot != null ? typeAnnot.accept() : null);
String[] value2 = annot.accept();
@ -249,12 +249,12 @@ final class HttpServiceMethod { @@ -249,12 +249,12 @@ final class HttpServiceMethod {
* return type blocking if necessary.
*/
private record ResponseFunction(
Function<HttpRequestDefinition, Publisher<?>> responseFunction,
Function<HttpRequestSpec, Publisher<?>> responseFunction,
@Nullable ReactiveAdapter returnTypeAdapter,
boolean blockForOptional, Duration blockTimeout) {
private ResponseFunction(
Function<HttpRequestDefinition, Publisher<?>> responseFunction,
Function<HttpRequestSpec, Publisher<?>> responseFunction,
@Nullable ReactiveAdapter returnTypeAdapter,
boolean blockForOptional, Duration blockTimeout) {
@ -265,9 +265,9 @@ final class HttpServiceMethod { @@ -265,9 +265,9 @@ final class HttpServiceMethod {
}
@Nullable
public Object execute(HttpRequestDefinition requestDefinition) {
public Object execute(HttpRequestSpec requestSpec) {
Publisher<?> responsePublisher = this.responseFunction.apply(requestDefinition);
Publisher<?> responsePublisher = this.responseFunction.apply(requestSpec);
if (this.returnTypeAdapter != null) {
return this.returnTypeAdapter.fromPublisher(responsePublisher);
@ -293,7 +293,7 @@ final class HttpServiceMethod { @@ -293,7 +293,7 @@ final class HttpServiceMethod {
MethodParameter actualParam = (reactiveAdapter != null ? returnParam.nested() : returnParam.nestedIfOptional());
Class<?> actualType = actualParam.getNestedParameterType();
Function<HttpRequestDefinition, Publisher<?>> responseFunction;
Function<HttpRequestSpec, Publisher<?>> responseFunction;
if (actualType.equals(void.class) || actualType.equals(Void.class)) {
responseFunction = client::requestToVoid;
}
@ -323,7 +323,7 @@ final class HttpServiceMethod { @@ -323,7 +323,7 @@ final class HttpServiceMethod {
}
@SuppressWarnings("ConstantConditions")
private static Function<HttpRequestDefinition, Publisher<?>> initResponseEntityFunction(
private static Function<HttpRequestSpec, Publisher<?>> initResponseEntityFunction(
HttpClientAdapter client, MethodParameter methodParam, @Nullable ReactiveAdapter reactiveAdapter) {
if (reactiveAdapter == null) {
@ -349,7 +349,7 @@ final class HttpServiceMethod { @@ -349,7 +349,7 @@ final class HttpServiceMethod {
});
}
private static Function<HttpRequestDefinition, Publisher<?>> initBodyFunction(
private static Function<HttpRequestSpec, Publisher<?>> initBodyFunction(
HttpClientAdapter client, MethodParameter methodParam, @Nullable ReactiveAdapter reactiveAdapter) {
ParameterizedTypeReference<?> bodyType =

20
spring-web/src/main/java/org/springframework/web/service/invoker/HttpServiceProxyFactory.java

@ -29,18 +29,18 @@ import org.springframework.aop.framework.ProxyFactory; @@ -29,18 +29,18 @@ import org.springframework.aop.framework.ProxyFactory;
import org.springframework.core.MethodIntrospector;
import org.springframework.core.ReactiveAdapterRegistry;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.web.service.annotation.HttpRequest;
import org.springframework.web.service.annotation.HttpExchange;
/**
* Factory to create a proxy for an HTTP service with {@link HttpRequest} methods.
* Factory to create a proxy for an HTTP service with {@link HttpExchange} methods.
*
* @author Rossen Stoyanchev
* @since 6.0
*/
public class HttpServiceProxyFactory {
private final List<HttpServiceMethodArgumentResolver> argumentResolvers;
private final List<HttpServiceArgumentResolver> argumentResolvers;
private final HttpClientAdapter clientAdapter;
@ -50,7 +50,7 @@ public class HttpServiceProxyFactory { @@ -50,7 +50,7 @@ public class HttpServiceProxyFactory {
public HttpServiceProxyFactory(
List<HttpServiceMethodArgumentResolver> argumentResolvers, HttpClientAdapter clientAdapter,
List<HttpServiceArgumentResolver> argumentResolvers, HttpClientAdapter clientAdapter,
ReactiveAdapterRegistry reactiveAdapterRegistry, Duration blockTimeout) {
this.argumentResolvers = argumentResolvers;
@ -66,7 +66,7 @@ public class HttpServiceProxyFactory { @@ -66,7 +66,7 @@ public class HttpServiceProxyFactory {
* @param <S> the service type
* @return the created proxy
*/
public <S> S createService(Class<S> serviceType) {
public <S> S createClient(Class<S> serviceType) {
List<HttpServiceMethod> methods =
MethodIntrospector.selectMethods(serviceType, this::isHttpRequestMethod)
@ -78,7 +78,7 @@ public class HttpServiceProxyFactory { @@ -78,7 +78,7 @@ public class HttpServiceProxyFactory {
}
private boolean isHttpRequestMethod(Method method) {
return AnnotatedElementUtils.hasAnnotation(method, HttpRequest.class);
return AnnotatedElementUtils.hasAnnotation(method, HttpExchange.class);
}
private HttpServiceMethod initServiceMethod(Method method, Class<?> serviceType) {
@ -93,16 +93,16 @@ public class HttpServiceProxyFactory { @@ -93,16 +93,16 @@ public class HttpServiceProxyFactory {
*/
private static final class HttpServiceMethodInterceptor implements MethodInterceptor {
private final Map<Method, HttpServiceMethod> serviceMethodMap = new HashMap<>();
private final Map<Method, HttpServiceMethod> httpServiceMethods = new HashMap<>();
private HttpServiceMethodInterceptor(List<HttpServiceMethod> methods) {
methods.forEach(serviceMethod -> this.serviceMethodMap.put(serviceMethod.getMethod(), serviceMethod));
methods.forEach(serviceMethod -> this.httpServiceMethods.put(serviceMethod.getMethod(), serviceMethod));
}
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
public Object invoke(MethodInvocation invocation) {
Method method = invocation.getMethod();
HttpServiceMethod httpServiceMethod = this.serviceMethodMap.get(method);
HttpServiceMethod httpServiceMethod = this.httpServiceMethods.get(method);
return httpServiceMethod.invoke(invocation.getArguments());
}

14
spring-web/src/main/java/org/springframework/web/service/invoker/PathVariableArgumentResolver.java

@ -31,7 +31,7 @@ import org.springframework.web.bind.annotation.PathVariable; @@ -31,7 +31,7 @@ import org.springframework.web.bind.annotation.PathVariable;
/**
* An implementation of {@link HttpServiceMethodArgumentResolver} that resolves
* An implementation of {@link HttpServiceArgumentResolver} that resolves
* request path variables based on method arguments annotated
* with {@link PathVariable}. {@code null} values are allowed only
* if {@link PathVariable#required()} is {@code true}.
@ -39,7 +39,7 @@ import org.springframework.web.bind.annotation.PathVariable; @@ -39,7 +39,7 @@ import org.springframework.web.bind.annotation.PathVariable;
* @author Olga Maciaszek-Sharma
* @since 6.0
*/
public class PathVariableArgumentResolver implements HttpServiceMethodArgumentResolver {
public class PathVariableArgumentResolver implements HttpServiceArgumentResolver {
private static final Log logger = LogFactory.getLog(PathVariableArgumentResolver.class);
@ -56,7 +56,7 @@ public class PathVariableArgumentResolver implements HttpServiceMethodArgumentRe @@ -56,7 +56,7 @@ public class PathVariableArgumentResolver implements HttpServiceMethodArgumentRe
@SuppressWarnings("unchecked")
@Override
public void resolve(
@Nullable Object argument, MethodParameter parameter, HttpRequestDefinition requestDefinition) {
@Nullable Object argument, MethodParameter parameter, HttpRequestSpec requestSpec) {
PathVariable annotation = parameter.getParameterAnnotation(PathVariable.class);
if (annotation == null) {
@ -67,19 +67,19 @@ public class PathVariableArgumentResolver implements HttpServiceMethodArgumentRe @@ -67,19 +67,19 @@ public class PathVariableArgumentResolver implements HttpServiceMethodArgumentRe
if (argument != null) {
Assert.isInstanceOf(Map.class, argument);
((Map<String, ?>) argument).forEach((key, value) ->
addUriParameter(key, value, annotation.required(), requestDefinition));
addUriParameter(key, value, annotation.required(), requestSpec));
}
}
else {
String name = StringUtils.hasText(annotation.value()) ? annotation.value() : annotation.name();
name = StringUtils.hasText(name) ? name : parameter.getParameterName();
Assert.notNull(name, "Failed to determine path variable name for parameter: " + parameter);
addUriParameter(name, argument, annotation.required(), requestDefinition);
addUriParameter(name, argument, annotation.required(), requestSpec);
}
}
private void addUriParameter(
String name, @Nullable Object value, boolean required, HttpRequestDefinition requestDefinition) {
String name, @Nullable Object value, boolean required, HttpRequestSpec requestSpec) {
if (value instanceof Optional) {
value = ((Optional<?>) value).orElse(null);
@ -98,7 +98,7 @@ public class PathVariableArgumentResolver implements HttpServiceMethodArgumentRe @@ -98,7 +98,7 @@ public class PathVariableArgumentResolver implements HttpServiceMethodArgumentRe
logger.trace("Resolved path variable '" + name + "' to " + value);
}
requestDefinition.getUriVariables().put(name, (String) value);
requestSpec.getUriVariables().put(name, (String) value);
}
}

2
spring-web/src/main/java/org/springframework/web/service/invoker/package-info.java

@ -1,6 +1,6 @@ @@ -1,6 +1,6 @@
/**
* Support to create a client proxy for an HTTP service annotated with
* {@link org.springframework.web.service.annotation.HttpRequest} methods.
* {@link org.springframework.web.service.annotation.HttpExchange} methods.
*/
@NonNullApi
@NonNullFields

16
spring-web/src/test/java/org/springframework/web/service/invoker/HttpMethodArgumentResolverTests.java

@ -20,8 +20,8 @@ import org.junit.jupiter.api.Test; @@ -20,8 +20,8 @@ import org.junit.jupiter.api.Test;
import org.springframework.http.HttpMethod;
import org.springframework.lang.Nullable;
import org.springframework.web.service.annotation.GetRequest;
import org.springframework.web.service.annotation.HttpRequest;
import org.springframework.web.service.annotation.GetExchange;
import org.springframework.web.service.annotation.HttpExchange;
import static org.assertj.core.api.Assertions.assertThat;
@ -64,25 +64,25 @@ public class HttpMethodArgumentResolverTests { @@ -64,25 +64,25 @@ public class HttpMethodArgumentResolverTests {
@Nullable
private HttpMethod getActualMethod() {
return this.clientAdapter.getRequestDefinition().getHttpMethod();
return this.clientAdapter.getRequestSpec().getHttpMethod();
}
private interface Service {
@HttpRequest
@HttpExchange
void execute(HttpMethod method);
@GetRequest
@GetExchange
void executeGet(HttpMethod method);
@HttpRequest
@HttpExchange
void execute(String test);
@HttpRequest
@HttpExchange
void execute(HttpMethod firstMethod, HttpMethod secondMethod);
@HttpRequest
@HttpExchange
void executeForNull(@Nullable HttpMethod method);
}

58
spring-web/src/test/java/org/springframework/web/service/invoker/HttpServiceMethodTests.java

@ -32,9 +32,9 @@ import org.springframework.http.HttpMethod; @@ -32,9 +32,9 @@ import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.lang.Nullable;
import org.springframework.web.service.annotation.GetRequest;
import org.springframework.web.service.annotation.HttpRequest;
import org.springframework.web.service.annotation.PostRequest;
import org.springframework.web.service.annotation.GetExchange;
import org.springframework.web.service.annotation.HttpExchange;
import org.springframework.web.service.annotation.PostExchange;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.http.MediaType.APPLICATION_CBOR_VALUE;
@ -145,7 +145,7 @@ public class HttpServiceMethodTests { @@ -145,7 +145,7 @@ public class HttpServiceMethodTests {
service.performGet();
HttpRequestDefinition request = this.clientAdapter.getRequestDefinition();
HttpRequestSpec request = this.clientAdapter.getRequestSpec();
assertThat(request.getHttpMethod()).isEqualTo(HttpMethod.GET);
assertThat(request.getUriTemplate()).isNull();
assertThat(request.getHeaders().getContentType()).isNull();
@ -153,7 +153,7 @@ public class HttpServiceMethodTests { @@ -153,7 +153,7 @@ public class HttpServiceMethodTests {
service.performPost();
request = this.clientAdapter.getRequestDefinition();
request = this.clientAdapter.getRequestSpec();
assertThat(request.getHttpMethod()).isEqualTo(HttpMethod.POST);
assertThat(request.getUriTemplate()).isEqualTo("/url");
assertThat(request.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_JSON);
@ -167,7 +167,7 @@ public class HttpServiceMethodTests { @@ -167,7 +167,7 @@ public class HttpServiceMethodTests {
service.performGet();
HttpRequestDefinition request = this.clientAdapter.getRequestDefinition();
HttpRequestSpec request = this.clientAdapter.getRequestSpec();
assertThat(request.getHttpMethod()).isEqualTo(HttpMethod.GET);
assertThat(request.getUriTemplate()).isEqualTo("/base");
assertThat(request.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_CBOR);
@ -175,7 +175,7 @@ public class HttpServiceMethodTests { @@ -175,7 +175,7 @@ public class HttpServiceMethodTests {
service.performPost();
request = this.clientAdapter.getRequestDefinition();
request = this.clientAdapter.getRequestSpec();
assertThat(request.getHttpMethod()).isEqualTo(HttpMethod.POST);
assertThat(request.getUriTemplate()).isEqualTo("/base/url");
assertThat(request.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_JSON);
@ -191,25 +191,25 @@ public class HttpServiceMethodTests { @@ -191,25 +191,25 @@ public class HttpServiceMethodTests {
@SuppressWarnings("unused")
private interface ReactorService {
@HttpRequest
@HttpExchange
Mono<Void> execute();
@GetRequest
@GetExchange
Mono<HttpHeaders> getHeaders();
@GetRequest
@GetExchange
Mono<String> getBody();
@GetRequest
@GetExchange
Flux<String> getFluxBody();
@GetRequest
@GetExchange
Mono<ResponseEntity<Void>> getVoidEntity();
@GetRequest
@GetExchange
Mono<ResponseEntity<String>> getEntity();
@GetRequest
@GetExchange
Mono<ResponseEntity<Flux<String>>> getFluxEntity();
}
@ -217,25 +217,25 @@ public class HttpServiceMethodTests { @@ -217,25 +217,25 @@ public class HttpServiceMethodTests {
@SuppressWarnings("unused")
private interface RxJavaService {
@HttpRequest
@HttpExchange
Completable execute();
@GetRequest
@GetExchange
Single<HttpHeaders> getHeaders();
@GetRequest
@GetExchange
Single<String> getBody();
@GetRequest
@GetExchange
Flowable<String> getFlowableBody();
@GetRequest
@GetExchange
Single<ResponseEntity<Void>> getVoidEntity();
@GetRequest
@GetExchange
Single<ResponseEntity<String>> getEntity();
@GetRequest
@GetExchange
Single<ResponseEntity<Flowable<String>>> getFlowableEntity();
}
@ -243,19 +243,19 @@ public class HttpServiceMethodTests { @@ -243,19 +243,19 @@ public class HttpServiceMethodTests {
@SuppressWarnings("unused")
private interface BlockingService {
@HttpRequest
@HttpExchange
void execute();
@GetRequest
@GetExchange
HttpHeaders getHeaders();
@GetRequest
@GetExchange
String getBody();
@GetRequest
@GetExchange
ResponseEntity<Void> getVoidEntity();
@GetRequest
@GetExchange
ResponseEntity<String> getEntity();
}
@ -263,17 +263,17 @@ public class HttpServiceMethodTests { @@ -263,17 +263,17 @@ public class HttpServiceMethodTests {
@SuppressWarnings("unused")
private interface MethodAnnotatedService {
@GetRequest
@GetExchange
void performGet();
@PostRequest(url = "/url", contentType = APPLICATION_JSON_VALUE, accept = APPLICATION_JSON_VALUE)
@PostExchange(url = "/url", contentType = APPLICATION_JSON_VALUE, accept = APPLICATION_JSON_VALUE)
void performPost();
}
@SuppressWarnings("unused")
@HttpRequest(url = "/base", contentType = APPLICATION_CBOR_VALUE, accept = APPLICATION_CBOR_VALUE)
@HttpExchange(url = "/base", contentType = APPLICATION_CBOR_VALUE, accept = APPLICATION_CBOR_VALUE)
private interface TypeAndMethodAnnotatedService extends MethodAnnotatedService {
}

26
spring-web/src/test/java/org/springframework/web/service/invoker/PathVariableArgumentResolverTests.java

@ -24,7 +24,7 @@ import org.junit.jupiter.api.Test; @@ -24,7 +24,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.service.annotation.HttpRequest;
import org.springframework.web.service.annotation.HttpExchange;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
@ -143,44 +143,44 @@ class PathVariableArgumentResolverTests { @@ -143,44 +143,44 @@ class PathVariableArgumentResolverTests {
}
private Map<String, String> getActualUriVariables() {
return this.clientAdapter.getRequestDefinition().getUriVariables();
return this.clientAdapter.getRequestSpec().getUriVariables();
}
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
private interface Service {
@HttpRequest
@HttpExchange
void execute(@PathVariable String id);
@HttpRequest
@HttpExchange
void executeNotRequired(@Nullable @PathVariable(required = false) String id);
@HttpRequest
@HttpExchange
void executeOptional(@PathVariable Optional<Boolean> id);
@HttpRequest
@HttpExchange
void executeOptionalNotRequired(@PathVariable(required = false) Optional<String> id);
@HttpRequest
@HttpExchange
void executeNamedWithValue(@Nullable @PathVariable(name = "test", value = "id") String employeeId);
@HttpRequest
@HttpExchange
void executeNamed(@PathVariable(name = "id") String employeeId);
@HttpRequest
@HttpExchange
void executeValueNamed(@PathVariable("id") String employeeId);
@HttpRequest
@HttpExchange
void execute(@PathVariable Object id);
@HttpRequest
@HttpExchange
void execute(@PathVariable Boolean id);
@HttpRequest
@HttpExchange
void executeValueMap(@Nullable @PathVariable Map<String, String> map);
@HttpRequest
@HttpExchange
void executeOptionalValueMap(@PathVariable Map<String, Optional<String>> map);
}

44
spring-web/src/test/java/org/springframework/web/service/invoker/TestHttpClientAdapter.java

@ -44,7 +44,7 @@ class TestHttpClientAdapter implements HttpClientAdapter { @@ -44,7 +44,7 @@ class TestHttpClientAdapter implements HttpClientAdapter {
private String invokedMethodName;
@Nullable
private HttpRequestDefinition requestDefinition;
private HttpRequestSpec requestSpec;
@Nullable
private ParameterizedTypeReference<?> bodyType;
@ -53,12 +53,12 @@ class TestHttpClientAdapter implements HttpClientAdapter { @@ -53,12 +53,12 @@ class TestHttpClientAdapter implements HttpClientAdapter {
/**
* Create the proxy for the give service type.
*/
public <S> S createService(Class<S> serviceType, HttpServiceMethodArgumentResolver... resolvers) {
public <S> S createService(Class<S> serviceType, HttpServiceArgumentResolver... resolvers) {
HttpServiceProxyFactory factory = new HttpServiceProxyFactory(
Arrays.asList(resolvers), this, ReactiveAdapterRegistry.getSharedInstance(), Duration.ofSeconds(5));
return factory.createService(serviceType);
return factory.createClient(serviceType);
}
@ -67,9 +67,9 @@ class TestHttpClientAdapter implements HttpClientAdapter { @@ -67,9 +67,9 @@ class TestHttpClientAdapter implements HttpClientAdapter {
return this.invokedMethodName;
}
public HttpRequestDefinition getRequestDefinition() {
assertThat(this.requestDefinition).isNotNull();
return this.requestDefinition;
public HttpRequestSpec getRequestSpec() {
assertThat(this.requestSpec).isNotNull();
return this.requestSpec;
}
@Nullable
@ -81,56 +81,56 @@ class TestHttpClientAdapter implements HttpClientAdapter { @@ -81,56 +81,56 @@ class TestHttpClientAdapter implements HttpClientAdapter {
// HttpClientAdapter implementation
@Override
public Mono<Void> requestToVoid(HttpRequestDefinition definition) {
saveInput("requestToVoid", definition, null);
public Mono<Void> requestToVoid(HttpRequestSpec requestSpec) {
saveInput("requestToVoid", requestSpec, null);
return Mono.empty();
}
@Override
public Mono<HttpHeaders> requestToHeaders(HttpRequestDefinition definition) {
saveInput("requestToHeaders", definition, null);
public Mono<HttpHeaders> requestToHeaders(HttpRequestSpec requestSpec) {
saveInput("requestToHeaders", requestSpec, null);
return Mono.just(new HttpHeaders());
}
@Override
public <T> Mono<T> requestToBody(HttpRequestDefinition definition, ParameterizedTypeReference<T> bodyType) {
saveInput("requestToBody", definition, bodyType);
public <T> Mono<T> requestToBody(HttpRequestSpec requestSpec, ParameterizedTypeReference<T> bodyType) {
saveInput("requestToBody", requestSpec, bodyType);
return (Mono<T>) Mono.just(getInvokedMethodName());
}
@Override
public <T> Flux<T> requestToBodyFlux(HttpRequestDefinition definition, ParameterizedTypeReference<T> bodyType) {
saveInput("requestToBodyFlux", definition, bodyType);
public <T> Flux<T> requestToBodyFlux(HttpRequestSpec requestSpec, ParameterizedTypeReference<T> bodyType) {
saveInput("requestToBodyFlux", requestSpec, bodyType);
return (Flux<T>) Flux.just("request", "To", "Body", "Flux");
}
@Override
public Mono<ResponseEntity<Void>> requestToBodilessEntity(HttpRequestDefinition definition) {
saveInput("requestToBodilessEntity", definition, null);
public Mono<ResponseEntity<Void>> requestToBodilessEntity(HttpRequestSpec requestSpec) {
saveInput("requestToBodilessEntity", requestSpec, null);
return Mono.just(ResponseEntity.ok().build());
}
@Override
public <T> Mono<ResponseEntity<T>> requestToEntity(
HttpRequestDefinition definition, ParameterizedTypeReference<T> type) {
HttpRequestSpec requestSpec, ParameterizedTypeReference<T> type) {
saveInput("requestToEntity", definition, type);
saveInput("requestToEntity", requestSpec, type);
return Mono.just((ResponseEntity<T>) ResponseEntity.ok("requestToEntity"));
}
@Override
public <T> Mono<ResponseEntity<Flux<T>>> requestToEntityFlux(
HttpRequestDefinition definition, ParameterizedTypeReference<T> bodyType) {
HttpRequestSpec requestSpec, ParameterizedTypeReference<T> bodyType) {
saveInput("requestToEntityFlux", definition, bodyType);
saveInput("requestToEntityFlux", requestSpec, bodyType);
return Mono.just(ResponseEntity.ok((Flux<T>) Flux.just("request", "To", "Entity", "Flux")));
}
private <T> void saveInput(
String methodName, HttpRequestDefinition definition, @Nullable ParameterizedTypeReference<T> bodyType) {
String methodName, HttpRequestSpec requestSpec, @Nullable ParameterizedTypeReference<T> bodyType) {
this.invokedMethodName = methodName;
this.requestDefinition = definition;
this.requestSpec = requestSpec;
this.bodyType = bodyType;
}

58
spring-webflux/src/main/java/org/springframework/web/reactive/function/client/support/WebClientAdapter.java

@ -27,7 +27,7 @@ import org.springframework.http.ResponseEntity; @@ -27,7 +27,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.web.reactive.function.client.ClientResponse;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.service.invoker.HttpClientAdapter;
import org.springframework.web.service.invoker.HttpRequestDefinition;
import org.springframework.web.service.invoker.HttpRequestSpec;
/**
@ -47,67 +47,67 @@ public class WebClientAdapter implements HttpClientAdapter { @@ -47,67 +47,67 @@ public class WebClientAdapter implements HttpClientAdapter {
@Override
public Mono<Void> requestToVoid(HttpRequestDefinition request) {
return toBodySpec(request).exchangeToMono(ClientResponse::releaseBody);
public Mono<Void> requestToVoid(HttpRequestSpec requestSpec) {
return toBodySpec(requestSpec).exchangeToMono(ClientResponse::releaseBody);
}
@Override
public Mono<HttpHeaders> requestToHeaders(HttpRequestDefinition request) {
return toBodySpec(request).retrieve().toBodilessEntity().map(ResponseEntity::getHeaders);
public Mono<HttpHeaders> requestToHeaders(HttpRequestSpec requestSpec) {
return toBodySpec(requestSpec).retrieve().toBodilessEntity().map(ResponseEntity::getHeaders);
}
@Override
public <T> Mono<T> requestToBody(HttpRequestDefinition request, ParameterizedTypeReference<T> bodyType) {
return toBodySpec(request).retrieve().bodyToMono(bodyType);
public <T> Mono<T> requestToBody(HttpRequestSpec reqrequestSpecest, ParameterizedTypeReference<T> bodyType) {
return toBodySpec(reqrequestSpecest).retrieve().bodyToMono(bodyType);
}
@Override
public <T> Flux<T> requestToBodyFlux(HttpRequestDefinition request, ParameterizedTypeReference<T> bodyType) {
return toBodySpec(request).retrieve().bodyToFlux(bodyType);
public <T> Flux<T> requestToBodyFlux(HttpRequestSpec requestSpec, ParameterizedTypeReference<T> bodyType) {
return toBodySpec(requestSpec).retrieve().bodyToFlux(bodyType);
}
@Override
public Mono<ResponseEntity<Void>> requestToBodilessEntity(HttpRequestDefinition request) {
return toBodySpec(request).retrieve().toBodilessEntity();
public Mono<ResponseEntity<Void>> requestToBodilessEntity(HttpRequestSpec requestSpec) {
return toBodySpec(requestSpec).retrieve().toBodilessEntity();
}
@Override
public <T> Mono<ResponseEntity<T>> requestToEntity(HttpRequestDefinition request, ParameterizedTypeReference<T> bodyType) {
return toBodySpec(request).retrieve().toEntity(bodyType);
public <T> Mono<ResponseEntity<T>> requestToEntity(HttpRequestSpec spec, ParameterizedTypeReference<T> bodyType) {
return toBodySpec(spec).retrieve().toEntity(bodyType);
}
@Override
public <T> Mono<ResponseEntity<Flux<T>>> requestToEntityFlux(HttpRequestDefinition request, ParameterizedTypeReference<T> bodyType) {
return toBodySpec(request).retrieve().toEntityFlux(bodyType);
public <T> Mono<ResponseEntity<Flux<T>>> requestToEntityFlux(HttpRequestSpec spec, ParameterizedTypeReference<T> bodyType) {
return toBodySpec(spec).retrieve().toEntityFlux(bodyType);
}
@SuppressWarnings("ReactiveStreamsUnusedPublisher")
private WebClient.RequestBodySpec toBodySpec(HttpRequestDefinition request) {
private WebClient.RequestBodySpec toBodySpec(HttpRequestSpec requestSpec) {
HttpMethod httpMethod = request.getHttpMethodRequired();
HttpMethod httpMethod = requestSpec.getHttpMethodRequired();
WebClient.RequestBodyUriSpec uriSpec = this.webClient.method(httpMethod);
WebClient.RequestBodySpec bodySpec;
if (request.getUri() != null) {
bodySpec = uriSpec.uri(request.getUri());
if (requestSpec.getUri() != null) {
bodySpec = uriSpec.uri(requestSpec.getUri());
}
else if (request.getUriTemplate() != null) {
bodySpec = (!request.getUriVariables().isEmpty() ?
uriSpec.uri(request.getUriTemplate(), request.getUriVariables()) :
uriSpec.uri(request.getUriTemplate(), request.getUriVariableValues()));
else if (requestSpec.getUriTemplate() != null) {
bodySpec = (!requestSpec.getUriVariables().isEmpty() ?
uriSpec.uri(requestSpec.getUriTemplate(), requestSpec.getUriVariables()) :
uriSpec.uri(requestSpec.getUriTemplate(), requestSpec.getUriVariableValues()));
}
else {
bodySpec = uriSpec.uri("");
}
bodySpec.headers(headers -> headers.putAll(request.getHeaders()));
bodySpec.cookies(cookies -> cookies.putAll(request.getCookies()));
bodySpec.headers(headers -> headers.putAll(requestSpec.getHeaders()));
bodySpec.cookies(cookies -> cookies.putAll(requestSpec.getCookies()));
if (request.getBodyValue() != null) {
bodySpec.bodyValue(request.getBodyValue());
if (requestSpec.getBodyValue() != null) {
bodySpec.bodyValue(requestSpec.getBodyValue());
}
else if (request.getBodyPublisher() != null) {
bodySpec.body(request.getBodyPublisher(), request.getBodyPublisherElementType());
else if (requestSpec.getBodyPublisher() != null) {
bodySpec.body(requestSpec.getBodyPublisher(), requestSpec.getBodyPublisherElementType());
}
return bodySpec;

6
spring-webflux/src/test/java/org/springframework/web/reactive/function/client/support/WebClientHttpServiceProxyTests.java

@ -33,7 +33,7 @@ import reactor.test.StepVerifier; @@ -33,7 +33,7 @@ import reactor.test.StepVerifier;
import org.springframework.core.ReactiveAdapterRegistry;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.service.annotation.GetRequest;
import org.springframework.web.service.annotation.GetExchange;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
@ -65,7 +65,7 @@ public class WebClientHttpServiceProxyTests { @@ -65,7 +65,7 @@ public class WebClientHttpServiceProxyTests {
Collections.emptyList(), webClientAdapter, ReactiveAdapterRegistry.getSharedInstance(),
Duration.ofSeconds(5));
this.httpService = proxyFactory.createService(TestHttpService.class);
this.httpService = proxyFactory.createClient(TestHttpService.class);
}
@SuppressWarnings("ConstantConditions")
@ -98,7 +98,7 @@ public class WebClientHttpServiceProxyTests { @@ -98,7 +98,7 @@ public class WebClientHttpServiceProxyTests {
private interface TestHttpService {
@GetRequest("/greeting")
@GetExchange("/greeting")
Mono<String> getGreeting();
}

Loading…
Cancel
Save