Support @RequestPart for @HttpExchange methods

Closes gh-29420
This commit is contained in:
rstoyanchev
2022-11-02 13:26:22 +00:00
parent 4b647a1801
commit 481389f761
6 changed files with 212 additions and 0 deletions

View File

@@ -24,6 +24,7 @@ import java.lang.annotation.Target;
import org.springframework.aot.hint.annotation.Reflective;
import org.springframework.core.annotation.AliasFor;
import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.Mapping;
/**
@@ -92,6 +93,17 @@ import org.springframework.web.bind.annotation.Mapping;
* RequestParamArgumentResolver}</td>
* </tr>
* <tr>
* <td>{@link org.springframework.web.bind.annotation.RequestPart @RequestPart}</td>
* <td>Add a request part, which may be a String (form field),
* {@link org.springframework.core.io.Resource} (file part), Object (entity to be
* encoded, e.g. as JSON), {@link HttpEntity} (part content and headers), a
* {@link org.springframework.http.codec.multipart.Part}, or a
* {@link org.reactivestreams.Publisher} of any of the above.
* (</td>
* <td>{@link org.springframework.web.service.invoker.RequestPartArgumentResolver
* RequestPartArgumentResolver}</td>
* </tr>
* <tr>
* <td>{@link org.springframework.web.bind.annotation.CookieValue @CookieValue}</td>
* <td>Add a cookie</td>
* <td>{@link org.springframework.web.service.invoker.CookieValueArgumentResolver

View File

@@ -29,9 +29,11 @@ import java.util.function.Function;
import org.reactivestreams.Publisher;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.ResolvableType;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.client.MultipartBodyBuilder;
import org.springframework.http.codec.FormHttpMessageWriter;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
@@ -222,6 +224,9 @@ public final class HttpRequestValues {
@Nullable
private MultiValueMap<String, String> requestParams;
@Nullable
private MultipartBodyBuilder multipartBuilder;
@Nullable
private Map<String, Object> attributes;
@@ -335,6 +340,26 @@ public final class HttpRequestValues {
return this;
}
/**
* Add a part to a multipart request. The part value may be as described
* in {@link MultipartBodyBuilder#part(String, Object)}.
*/
public Builder addRequestPart(String name, Object part) {
this.multipartBuilder = (this.multipartBuilder != null ? this.multipartBuilder : new MultipartBodyBuilder());
this.multipartBuilder.part(name, part);
return this;
}
/**
* Variant of {@link #addRequestPart(String, Object)} that allows the
* part value to be produced by a {@link Publisher}.
*/
public <T, P extends Publisher<T>> Builder addRequestPart(String name, P publisher, ResolvableType type) {
this.multipartBuilder = (this.multipartBuilder != null ? this.multipartBuilder : new MultipartBodyBuilder());
this.multipartBuilder.asyncPart(name, publisher, ParameterizedTypeReference.forType(type.getType()));
return this;
}
/**
* Configure an attribute to associate with the request.
* @param name the attribute name
@@ -399,6 +424,10 @@ public final class HttpRequestValues {
uriTemplate = appendQueryParams(uriTemplate, uriVars, this.requestParams);
}
}
else if (this.multipartBuilder != null) {
Assert.isTrue(bodyValue == null && this.body == null, "Expected body or request parts, not both");
bodyValue = this.multipartBuilder.build();
}
HttpHeaders headers = HttpHeaders.EMPTY;
if (this.headers != null) {

View File

@@ -323,6 +323,7 @@ public final class HttpServiceProxyFactory implements InitializingBean, Embedded
resolvers.add(new RequestBodyArgumentResolver(this.reactiveAdapterRegistry));
resolvers.add(new PathVariableArgumentResolver(service));
resolvers.add(new RequestParamArgumentResolver(service));
resolvers.add(new RequestPartArgumentResolver(this.reactiveAdapterRegistry));
resolvers.add(new CookieValueArgumentResolver(service));
resolvers.add(new RequestAttributeArgumentResolver());
@@ -497,6 +498,7 @@ public final class HttpServiceProxyFactory implements InitializingBean, Embedded
resolvers.add(new RequestBodyArgumentResolver(this.reactiveAdapterRegistry));
resolvers.add(new PathVariableArgumentResolver(conversionService));
resolvers.add(new RequestParamArgumentResolver(conversionService));
resolvers.add(new RequestPartArgumentResolver(this.reactiveAdapterRegistry));
resolvers.add(new CookieValueArgumentResolver(conversionService));
resolvers.add(new RequestAttributeArgumentResolver());

View File

@@ -0,0 +1,81 @@
/*
* Copyright 2002-2022 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.reactivestreams.Publisher;
import org.springframework.core.MethodParameter;
import org.springframework.core.ReactiveAdapter;
import org.springframework.core.ReactiveAdapterRegistry;
import org.springframework.core.ResolvableType;
import org.springframework.http.HttpEntity;
import org.springframework.http.codec.multipart.Part;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestPart;
/**
* {@link HttpServiceArgumentResolver} for {@link RequestPart @RequestPart}
* annotated arguments.
*
* <p>The argument may be:
* <ul>
* <li>String -- form field
* <li>{@link org.springframework.core.io.Resource Resource} -- file part
* <li>Object -- content to be encoded (e.g. to JSON)
* <li>{@link HttpEntity} -- part content and headers although generally it's
* easier to add headers through the returned builder
* <li>{@link Part} -- a part from a server request
* <li>{@link Publisher} of any of the above
* </ul>
*
* @author Rossen Stoyanchev
* @since 6.0
*/
public class RequestPartArgumentResolver extends AbstractNamedValueArgumentResolver {
private final ReactiveAdapterRegistry reactiveAdapterRegistry;
public RequestPartArgumentResolver(ReactiveAdapterRegistry reactiveAdapterRegistry) {
this.reactiveAdapterRegistry = reactiveAdapterRegistry;
}
@Override
protected NamedValueInfo createNamedValueInfo(MethodParameter parameter) {
RequestPart annot = parameter.getParameterAnnotation(RequestPart.class);
return (annot == null ? null :
new NamedValueInfo(annot.name(), annot.required(), null, "request part", true));
}
@Override
protected void addRequestValue(
String name, Object value, MethodParameter parameter, HttpRequestValues.Builder requestValues) {
Class<?> type = parameter.getParameterType();
ReactiveAdapter adapter = this.reactiveAdapterRegistry.getAdapter(type);
if (adapter != null) {
Assert.isTrue(!adapter.isNoValue(), "Expected publisher that produces a value");
Publisher<?> publisher = adapter.toPublisher(value);
requestValues.addRequestPart(name, publisher, ResolvableType.forMethodParameter(parameter.nested()));
}
else {
requestValues.addRequestPart(name, value);
}
}
}