Support any HttpEntity implementing ResolvableTypeProvider

Closes gh-22931
This commit is contained in:
Juergen Hoeller 2019-05-09 02:09:31 +02:00
parent c4a95c99e5
commit 190b751147
2 changed files with 12 additions and 11 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,9 +25,11 @@ import org.reactivestreams.Publisher;
import org.springframework.core.ParameterizedTypeReference; import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.ResolvableType; import org.springframework.core.ResolvableType;
import org.springframework.core.ResolvableTypeProvider;
import org.springframework.http.HttpEntity; import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.LinkedMultiValueMap;
@ -263,13 +265,13 @@ public final class MultipartBodyBuilder {
* @param <T> the type contained in the publisher * @param <T> the type contained in the publisher
* @param <P> the publisher * @param <P> the publisher
*/ */
public static final class PublisherEntity<T, P extends Publisher<T>> extends HttpEntity<P> { public static final class PublisherEntity<T, P extends Publisher<T>> extends HttpEntity<P>
implements ResolvableTypeProvider {
private final ResolvableType resolvableType; private final ResolvableType resolvableType;
PublisherEntity(
private PublisherEntity(@Nullable MultiValueMap<String, String> headers, P publisher, @Nullable MultiValueMap<String, String> headers, P publisher, ResolvableType resolvableType) {
ResolvableType resolvableType) {
super(publisher, headers); super(publisher, headers);
Assert.notNull(publisher, "'publisher' must not be null"); Assert.notNull(publisher, "'publisher' must not be null");
@ -280,6 +282,8 @@ public final class MultipartBodyBuilder {
/** /**
* Return the element type for the {@code Publisher} body. * Return the element type for the {@code Publisher} body.
*/ */
@Override
@NonNull
public ResolvableType getResolvableType() { public ResolvableType getResolvableType() {
return this.resolvableType; return this.resolvableType;
} }

View File

@ -34,6 +34,7 @@ import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import org.springframework.core.ResolvableType; import org.springframework.core.ResolvableType;
import org.springframework.core.ResolvableTypeProvider;
import org.springframework.core.codec.CharSequenceEncoder; import org.springframework.core.codec.CharSequenceEncoder;
import org.springframework.core.codec.CodecException; import org.springframework.core.codec.CodecException;
import org.springframework.core.codec.Hints; import org.springframework.core.codec.Hints;
@ -46,7 +47,6 @@ import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.ReactiveHttpOutputMessage; import org.springframework.http.ReactiveHttpOutputMessage;
import org.springframework.http.client.MultipartBodyBuilder;
import org.springframework.http.codec.EncoderHttpMessageWriter; import org.springframework.http.codec.EncoderHttpMessageWriter;
import org.springframework.http.codec.FormHttpMessageWriter; import org.springframework.http.codec.FormHttpMessageWriter;
import org.springframework.http.codec.HttpMessageWriter; import org.springframework.http.codec.HttpMessageWriter;
@ -265,11 +265,8 @@ public class MultipartHttpMessageWriter extends LoggingCodecSupport
outputHeaders.putAll(httpEntity.getHeaders()); outputHeaders.putAll(httpEntity.getHeaders());
body = httpEntity.getBody(); body = httpEntity.getBody();
Assert.state(body != null, "MultipartHttpMessageWriter only supports HttpEntity with body"); Assert.state(body != null, "MultipartHttpMessageWriter only supports HttpEntity with body");
if (httpEntity instanceof ResolvableTypeProvider) {
if (httpEntity instanceof MultipartBodyBuilder.PublisherEntity<?, ?>) { resolvableType = ((ResolvableTypeProvider) httpEntity).getResolvableType();
MultipartBodyBuilder.PublisherEntity<?, ?> publisherEntity =
(MultipartBodyBuilder.PublisherEntity<?, ?>) httpEntity;
resolvableType = publisherEntity.getResolvableType();
} }
} }
else { else {