Skip to content

Commit 481389f

Browse files
committed
Support @RequestPart for @HttpExchange methods
Closes gh-29420
1 parent 4b647a1 commit 481389f

File tree

6 files changed

+212
-0
lines changed

6 files changed

+212
-0
lines changed

framework-docs/src/docs/asciidoc/integration.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,11 @@ method parameters:
453453
parameters are encoded in the request body. Otherwise, they are added as URL query
454454
parameters.
455455

456+
| `@RequestPart`
457+
| Add a request part, which may be a String (form field), `Resource` (file part),
458+
Object (entity to be encoded, e.g. as JSON), `HttpEntity` (part content and headers),
459+
a Spring `Part`, or Reactive Streams `Publisher` of any of the above.
460+
456461
| `@CookieValue`
457462
| Add a cookie or mutliple cookies. The argument may be a `Map<String, ?>` or
458463
`MultiValueMap<String, ?>` with multiple cookies, a `Collection<?>` of values, or an

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import org.springframework.aot.hint.annotation.Reflective;
2626
import org.springframework.core.annotation.AliasFor;
27+
import org.springframework.http.HttpEntity;
2728
import org.springframework.web.bind.annotation.Mapping;
2829

2930
/**
@@ -92,6 +93,17 @@
9293
* RequestParamArgumentResolver}</td>
9394
* </tr>
9495
* <tr>
96+
* <td>{@link org.springframework.web.bind.annotation.RequestPart @RequestPart}</td>
97+
* <td>Add a request part, which may be a String (form field),
98+
* {@link org.springframework.core.io.Resource} (file part), Object (entity to be
99+
* encoded, e.g. as JSON), {@link HttpEntity} (part content and headers), a
100+
* {@link org.springframework.http.codec.multipart.Part}, or a
101+
* {@link org.reactivestreams.Publisher} of any of the above.
102+
* (</td>
103+
* <td>{@link org.springframework.web.service.invoker.RequestPartArgumentResolver
104+
* RequestPartArgumentResolver}</td>
105+
* </tr>
106+
* <tr>
95107
* <td>{@link org.springframework.web.bind.annotation.CookieValue @CookieValue}</td>
96108
* <td>Add a cookie</td>
97109
* <td>{@link org.springframework.web.service.invoker.CookieValueArgumentResolver

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@
2929
import org.reactivestreams.Publisher;
3030

3131
import org.springframework.core.ParameterizedTypeReference;
32+
import org.springframework.core.ResolvableType;
3233
import org.springframework.http.HttpHeaders;
3334
import org.springframework.http.HttpMethod;
3435
import org.springframework.http.MediaType;
36+
import org.springframework.http.client.MultipartBodyBuilder;
3537
import org.springframework.http.codec.FormHttpMessageWriter;
3638
import org.springframework.lang.Nullable;
3739
import org.springframework.util.Assert;
@@ -222,6 +224,9 @@ public final static class Builder {
222224
@Nullable
223225
private MultiValueMap<String, String> requestParams;
224226

227+
@Nullable
228+
private MultipartBodyBuilder multipartBuilder;
229+
225230
@Nullable
226231
private Map<String, Object> attributes;
227232

@@ -335,6 +340,26 @@ public Builder addRequestParameter(String name, String... values) {
335340
return this;
336341
}
337342

343+
/**
344+
* Add a part to a multipart request. The part value may be as described
345+
* in {@link MultipartBodyBuilder#part(String, Object)}.
346+
*/
347+
public Builder addRequestPart(String name, Object part) {
348+
this.multipartBuilder = (this.multipartBuilder != null ? this.multipartBuilder : new MultipartBodyBuilder());
349+
this.multipartBuilder.part(name, part);
350+
return this;
351+
}
352+
353+
/**
354+
* Variant of {@link #addRequestPart(String, Object)} that allows the
355+
* part value to be produced by a {@link Publisher}.
356+
*/
357+
public <T, P extends Publisher<T>> Builder addRequestPart(String name, P publisher, ResolvableType type) {
358+
this.multipartBuilder = (this.multipartBuilder != null ? this.multipartBuilder : new MultipartBodyBuilder());
359+
this.multipartBuilder.asyncPart(name, publisher, ParameterizedTypeReference.forType(type.getType()));
360+
return this;
361+
}
362+
338363
/**
339364
* Configure an attribute to associate with the request.
340365
* @param name the attribute name
@@ -399,6 +424,10 @@ else if (uri != null) {
399424
uriTemplate = appendQueryParams(uriTemplate, uriVars, this.requestParams);
400425
}
401426
}
427+
else if (this.multipartBuilder != null) {
428+
Assert.isTrue(bodyValue == null && this.body == null, "Expected body or request parts, not both");
429+
bodyValue = this.multipartBuilder.build();
430+
}
402431

403432
HttpHeaders headers = HttpHeaders.EMPTY;
404433
if (this.headers != null) {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ private List<HttpServiceArgumentResolver> initArgumentResolvers() {
323323
resolvers.add(new RequestBodyArgumentResolver(this.reactiveAdapterRegistry));
324324
resolvers.add(new PathVariableArgumentResolver(service));
325325
resolvers.add(new RequestParamArgumentResolver(service));
326+
resolvers.add(new RequestPartArgumentResolver(this.reactiveAdapterRegistry));
326327
resolvers.add(new CookieValueArgumentResolver(service));
327328
resolvers.add(new RequestAttributeArgumentResolver());
328329

@@ -497,6 +498,7 @@ private List<HttpServiceArgumentResolver> initArgumentResolvers(ConversionServic
497498
resolvers.add(new RequestBodyArgumentResolver(this.reactiveAdapterRegistry));
498499
resolvers.add(new PathVariableArgumentResolver(conversionService));
499500
resolvers.add(new RequestParamArgumentResolver(conversionService));
501+
resolvers.add(new RequestPartArgumentResolver(this.reactiveAdapterRegistry));
500502
resolvers.add(new CookieValueArgumentResolver(conversionService));
501503
resolvers.add(new RequestAttributeArgumentResolver());
502504

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.web.service.invoker;
18+
19+
import org.reactivestreams.Publisher;
20+
21+
import org.springframework.core.MethodParameter;
22+
import org.springframework.core.ReactiveAdapter;
23+
import org.springframework.core.ReactiveAdapterRegistry;
24+
import org.springframework.core.ResolvableType;
25+
import org.springframework.http.HttpEntity;
26+
import org.springframework.http.codec.multipart.Part;
27+
import org.springframework.util.Assert;
28+
import org.springframework.web.bind.annotation.RequestPart;
29+
30+
/**
31+
* {@link HttpServiceArgumentResolver} for {@link RequestPart @RequestPart}
32+
* annotated arguments.
33+
*
34+
* <p>The argument may be:
35+
* <ul>
36+
* <li>String -- form field
37+
* <li>{@link org.springframework.core.io.Resource Resource} -- file part
38+
* <li>Object -- content to be encoded (e.g. to JSON)
39+
* <li>{@link HttpEntity} -- part content and headers although generally it's
40+
* easier to add headers through the returned builder
41+
* <li>{@link Part} -- a part from a server request
42+
* <li>{@link Publisher} of any of the above
43+
* </ul>
44+
*
45+
* @author Rossen Stoyanchev
46+
* @since 6.0
47+
*/
48+
public class RequestPartArgumentResolver extends AbstractNamedValueArgumentResolver {
49+
50+
private final ReactiveAdapterRegistry reactiveAdapterRegistry;
51+
52+
53+
public RequestPartArgumentResolver(ReactiveAdapterRegistry reactiveAdapterRegistry) {
54+
this.reactiveAdapterRegistry = reactiveAdapterRegistry;
55+
}
56+
57+
58+
@Override
59+
protected NamedValueInfo createNamedValueInfo(MethodParameter parameter) {
60+
RequestPart annot = parameter.getParameterAnnotation(RequestPart.class);
61+
return (annot == null ? null :
62+
new NamedValueInfo(annot.name(), annot.required(), null, "request part", true));
63+
}
64+
65+
@Override
66+
protected void addRequestValue(
67+
String name, Object value, MethodParameter parameter, HttpRequestValues.Builder requestValues) {
68+
69+
Class<?> type = parameter.getParameterType();
70+
ReactiveAdapter adapter = this.reactiveAdapterRegistry.getAdapter(type);
71+
if (adapter != null) {
72+
Assert.isTrue(!adapter.isNoValue(), "Expected publisher that produces a value");
73+
Publisher<?> publisher = adapter.toPublisher(value);
74+
requestValues.addRequestPart(name, publisher, ResolvableType.forMethodParameter(parameter.nested()));
75+
}
76+
else {
77+
requestValues.addRequestPart(name, value);
78+
}
79+
}
80+
81+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.web.service.invoker;
18+
19+
import org.junit.jupiter.api.BeforeEach;
20+
import org.junit.jupiter.api.Test;
21+
import reactor.core.publisher.Mono;
22+
23+
import org.springframework.http.HttpEntity;
24+
import org.springframework.http.HttpHeaders;
25+
import org.springframework.util.MultiValueMap;
26+
import org.springframework.web.bind.annotation.RequestPart;
27+
import org.springframework.web.service.annotation.PostExchange;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
31+
/**
32+
* Unit tests for {@link RequestPartArgumentResolver}.
33+
*
34+
* <p>Additional tests for this resolver:
35+
* <ul>
36+
* <li>Base class functionality in {@link NamedValueArgumentResolverTests}
37+
* <li>Form data vs query params in {@link HttpRequestValuesTests}
38+
* </ul>
39+
*
40+
* @author Rossen Stoyanchev
41+
*/
42+
public class RequestPartArgumentResolverTests {
43+
44+
private final TestHttpClientAdapter client = new TestHttpClientAdapter();
45+
46+
private Service service;
47+
48+
49+
@BeforeEach
50+
void setUp() throws Exception {
51+
HttpServiceProxyFactory proxyFactory = HttpServiceProxyFactory.builder(this.client).build();
52+
this.service = proxyFactory.createClient(Service.class);
53+
}
54+
55+
56+
// Base class functionality should be tested in NamedValueArgumentResolverTests.
57+
// Form data vs query params tested in HttpRequestValuesTests.
58+
59+
@Test
60+
void requestPart() {
61+
HttpHeaders headers = new HttpHeaders();
62+
headers.add("foo", "bar");
63+
HttpEntity<String> part2 = new HttpEntity<>("part 2", headers);
64+
this.service.postMultipart("part 1", part2, Mono.just("part 3"));
65+
66+
Object body = this.client.getRequestValues().getBodyValue();
67+
assertThat(body).isNotNull().isInstanceOf(MultiValueMap.class);
68+
MultiValueMap<String, HttpEntity<?>> map = (MultiValueMap<String, HttpEntity<?>>) body;
69+
70+
assertThat(map.getFirst("part1").getBody()).isEqualTo("part 1");
71+
assertThat(map.getFirst("part2")).isEqualTo(part2);
72+
assertThat(((Mono<?>) map.getFirst("part3").getBody()).block()).isEqualTo("part 3");
73+
}
74+
75+
76+
private interface Service {
77+
78+
@PostExchange
79+
void postMultipart(@RequestPart String part1, @RequestPart HttpEntity<String> part2, @RequestPart Mono<String> part3);
80+
81+
}
82+
83+
}

0 commit comments

Comments
 (0)