Support for decoding @RequestPart content
Issue: SPR-15515
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
package org.springframework.http.codec;
|
||||
|
||||
import org.springframework.core.codec.Decoder;
|
||||
import org.springframework.core.codec.Encoder;
|
||||
|
||||
/**
|
||||
* Helps to configure a list of client-side HTTP message readers and writers
|
||||
@@ -55,6 +56,13 @@ public interface ClientCodecConfigurer extends CodecConfigurer {
|
||||
*/
|
||||
interface ClientDefaultCodecsConfigurer extends DefaultCodecsConfigurer {
|
||||
|
||||
/**
|
||||
* Configure encoders or writers for use with
|
||||
* {@link org.springframework.http.codec.multipart.MultipartHttpMessageWriter
|
||||
* MultipartHttpMessageWriter}.
|
||||
*/
|
||||
MultipartCodecsConfigurer multipartCodecs();
|
||||
|
||||
/**
|
||||
* Configure the {@code Decoder} to use for Server-Sent Events.
|
||||
* <p>By default the {@link #jackson2Decoder} override is used for SSE.
|
||||
@@ -63,5 +71,25 @@ public interface ClientCodecConfigurer extends CodecConfigurer {
|
||||
void serverSentEventDecoder(Decoder<?> decoder);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registry and container for multipart HTTP message writers.
|
||||
*/
|
||||
interface MultipartCodecsConfigurer {
|
||||
|
||||
/**
|
||||
* Add a Part {@code Encoder}, internally wrapped with
|
||||
* {@link EncoderHttpMessageWriter}.
|
||||
* @param encoder the encoder to add
|
||||
*/
|
||||
MultipartCodecsConfigurer encoder(Encoder<?> encoder);
|
||||
|
||||
/**
|
||||
* Add a Part {@link HttpMessageWriter}. For writers of type
|
||||
* {@link EncoderHttpMessageWriter} consider using the shortcut
|
||||
* {@link #encoder(Encoder)} instead.
|
||||
* @param writer the writer to add
|
||||
*/
|
||||
MultipartCodecsConfigurer writer(HttpMessageWriter<?> writer);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -108,7 +108,7 @@ public interface CodecConfigurer {
|
||||
void reader(HttpMessageReader<?> reader);
|
||||
|
||||
/**
|
||||
* Add a custom {@link HttpMessageWriter}. For readers of type
|
||||
* Add a custom {@link HttpMessageWriter}. For writers of type
|
||||
* {@link EncoderHttpMessageWriter} consider using the shortcut
|
||||
* {@link #encoder(Encoder)} instead.
|
||||
* @param writer the writer to add
|
||||
|
||||
@@ -16,9 +16,11 @@
|
||||
|
||||
package org.springframework.http.codec;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.core.codec.Decoder;
|
||||
import org.springframework.core.codec.Encoder;
|
||||
import org.springframework.core.codec.StringDecoder;
|
||||
import org.springframework.http.codec.json.Jackson2JsonDecoder;
|
||||
import org.springframework.http.codec.multipart.MultipartHttpMessageWriter;
|
||||
@@ -48,6 +50,17 @@ class DefaultClientCodecConfigurer extends DefaultCodecConfigurer implements Cli
|
||||
extends AbstractDefaultCodecsConfigurer
|
||||
implements ClientCodecConfigurer.ClientDefaultCodecsConfigurer {
|
||||
|
||||
private DefaultMultipartCodecsConfigurer multipartCodecs;
|
||||
|
||||
|
||||
@Override
|
||||
public MultipartCodecsConfigurer multipartCodecs() {
|
||||
if (this.multipartCodecs == null) {
|
||||
this.multipartCodecs = new DefaultMultipartCodecsConfigurer();
|
||||
}
|
||||
return this.multipartCodecs;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serverSentEventDecoder(Decoder<?> decoder) {
|
||||
HttpMessageReader<?> reader = new ServerSentEventHttpMessageReader(decoder);
|
||||
@@ -58,7 +71,10 @@ class DefaultClientCodecConfigurer extends DefaultCodecConfigurer implements Cli
|
||||
protected void addTypedWritersTo(List<HttpMessageWriter<?>> result) {
|
||||
super.addTypedWritersTo(result);
|
||||
addWriterTo(result, FormHttpMessageWriter::new);
|
||||
addWriterTo(result, MultipartHttpMessageWriter::new);
|
||||
addWriterTo(result, () -> findWriter(MultipartHttpMessageWriter.class,
|
||||
() -> this.multipartCodecs != null ?
|
||||
new MultipartHttpMessageWriter(this.multipartCodecs.getWriters()) :
|
||||
new MultipartHttpMessageWriter()));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -89,7 +105,28 @@ class DefaultClientCodecConfigurer extends DefaultCodecConfigurer implements Cli
|
||||
addReaderTo(result,
|
||||
() -> new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes(false)));
|
||||
}
|
||||
}
|
||||
|
||||
private static class DefaultMultipartCodecsConfigurer implements MultipartCodecsConfigurer {
|
||||
|
||||
private final List<HttpMessageWriter<?>> writers = new ArrayList<>();
|
||||
|
||||
|
||||
@Override
|
||||
public MultipartCodecsConfigurer encoder(Encoder<?> encoder) {
|
||||
writer(new EncoderHttpMessageWriter<>(encoder));
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MultipartCodecsConfigurer writer(HttpMessageWriter<?> writer) {
|
||||
this.writers.add(writer);
|
||||
return this;
|
||||
}
|
||||
|
||||
public List<HttpMessageWriter<?>> getWriters() {
|
||||
return this.writers;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user