Top Description Inners Fields Constructors Methods
org.springframework.http.codec.multipart

public Class MultipartHttpMessageWriter

extends MultipartWriterSupport
implements HttpMessageWriter<MultiValueMap<String, ?>>
Class Inheritance
All Implemented Interfaces
org.springframework.http.codec.HttpMessageWriter
Imports
java.util.ArrayList, .Arrays, .Collections, .List, .Map, .Optional, java.util.concurrent.atomic.AtomicBoolean, java.util.function.Supplier, org.reactivestreams.Publisher, reactor.core.publisher.Flux, .Mono, org.springframework.core.ResolvableType, .ResolvableTypeProvider, org.springframework.core.codec.CharSequenceEncoder, .CodecException, .Hints, org.springframework.core.io.Resource, org.springframework.core.io.buffer.DataBuffer, .DataBufferFactory, .DataBufferUtils, org.springframework.core.log.LogFormatUtils, org.springframework.http.HttpEntity, .HttpHeaders, .MediaType, .ReactiveHttpOutputMessage, org.springframework.http.codec.EncoderHttpMessageWriter, .FormHttpMessageWriter, .HttpMessageWriter, .ResourceHttpMessageWriter, org.springframework.lang.Nullable, org.springframework.util.Assert, .MultiValueMap

HttpMessageWriter for writing a MultiValueMap<String, ?> as multipart form data, i.e. "multipart/form-data", to the body of a request.

The serialization of individual parts is delegated to other writers. By default only String and Resource parts are supported but you can configure others through a constructor argument.

This writer can be configured with a FormHttpMessageWriter to delegate to. It is the preferred way of supporting both form data and multipart data (as opposed to registering each writer separately) so that when the MediaType is not specified and generics are not present on the target element type, we can inspect the values in the actual map and decide whether to write plain form data (String values only) or otherwise.

Authors
Sebastien Deleuze, Rossen Stoyanchev
Since
5.0
See Also
FormHttpMessageWriter

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class

Field Summary

Modifier and TypeField and Description
private static final Map<String, Object>
DEFAULT_HINTS

Suppress logging from individual part writers (full map logged at this level).

private final HttpMessageWriter<MultiValueMap<String, String>>
private final Supplier<List<HttpMessageWriter<?>>>
Inherited from org.springframework.http.codec.multipart.MultipartWriterSupport:
DEFAULT_CHARSET

Constructor Summary

AccessConstructor and Description
public
MultipartHttpMessageWriter()

Constructor with a default list of part writers (String and Resource).

public
MultipartHttpMessageWriter(List<HttpMessageWriter<?>> partWriters)

Constructor with explicit list of writers for serializing parts.

public
MultipartHttpMessageWriter(List<HttpMessageWriter<?>>
the writers for serializing parts
partWriters
,
HttpMessageWriter<MultiValueMap<String, String>>
the fallback writer for form data, null by default
formWriter
)

Constructor with explicit list of writers for serializing parts and a writer for plain form data to fall back when no media type is specified and the actual map consists of String values only.

public
MultipartHttpMessageWriter(Supplier<List<HttpMessageWriter<?>>>
the supplier for writers for serializing parts
partWritersSupplier
,
HttpMessageWriter<MultiValueMap<String, String>>
the fallback writer for form data, null by default
formWriter
)

Constructor with a supplier for an explicit list of writers for serializing parts and a writer for plain form data to fall back when no media type is specified and the actual map consists of String values only.

Method Summary

Modifier and TypeMethod and Description
public boolean
canWrite(ResolvableType
the type of object to check
elementType
,
MediaType
the media type for the write (possibly null)
mediaType
)

Implements org.springframework.http.codec.HttpMessageWriter.canWrite.

Whether the given object type is supported by this writer.
private <T> Flux<DataBuffer>
encodePart(byte[] boundary, String name, T value, DataBufferFactory factory)

private Flux<DataBuffer>
encodePartValues(byte[] boundary, String name, List<?> values, DataBufferFactory bufferFactory)

public HttpMessageWriter<MultiValueMap<String, String>>
getFormWriter()

Return the configured form writer.

public List<HttpMessageWriter<?>>
getPartWriters()

Return the configured part writers.

private static List<MediaType>
private boolean
isMultipart(MultiValueMap<String, ?> map, MediaType contentType)

public Mono<Void>
write(Publisher<? extends MultiValueMap<String, ?>>
the objects to write
inputStream
,
ResolvableType
the type of objects in the stream which must have been previously checked via canWrite(ResolvableType, MediaType)
elementType
,
MediaType
the content type for the write (possibly null to indicate that the default content type of the writer must be used)
mediaType
,
ReactiveHttpOutputMessage
the message to write to
outputMessage
,
Map<String, Object>
additional information about how to encode and write
hints
)

Implements org.springframework.http.codec.HttpMessageWriter.write.

Write a given stream of object to the output message.
private Mono<Void>
Inherited from org.springframework.http.codec.multipart.MultipartWriterSupport:
generateBoundaryLinegenerateLastLinegenerateMultipartBoundarygenerateNewLinegeneratePartHeadersgetCharsetgetMultipartMediaTypegetWritableMediaTypessetCharset

Field Detail

DEFAULT_HINTSback to summary
private static final Map<String, Object> DEFAULT_HINTS

Suppress logging from individual part writers (full map logged at this level).

formWriterback to summary
private final HttpMessageWriter<MultiValueMap<String, String>> formWriter
Annotations
@Nullable
partWritersSupplierback to summary
private final Supplier<List<HttpMessageWriter<?>>> partWritersSupplier

Constructor Detail

MultipartHttpMessageWriterback to summary
public MultipartHttpMessageWriter()

Constructor with a default list of part writers (String and Resource).

MultipartHttpMessageWriterback to summary
public MultipartHttpMessageWriter(List<HttpMessageWriter<?>> partWriters)

Constructor with explicit list of writers for serializing parts.

MultipartHttpMessageWriterback to summary
public MultipartHttpMessageWriter(List<HttpMessageWriter<?>> partWriters, HttpMessageWriter<MultiValueMap<String, String>> formWriter)

Constructor with explicit list of writers for serializing parts and a writer for plain form data to fall back when no media type is specified and the actual map consists of String values only.

Parameters
partWriters:List<HttpMessageWriter<?>>

the writers for serializing parts

formWriter:HttpMessageWriter<MultiValueMap<String, String>>

the fallback writer for form data, null by default

MultipartHttpMessageWriterback to summary
public MultipartHttpMessageWriter(Supplier<List<HttpMessageWriter<?>>> partWritersSupplier, HttpMessageWriter<MultiValueMap<String, String>> formWriter)

Constructor with a supplier for an explicit list of writers for serializing parts and a writer for plain form data to fall back when no media type is specified and the actual map consists of String values only.

Parameters
partWritersSupplier:Supplier<List<HttpMessageWriter<?>>>

the supplier for writers for serializing parts

formWriter:HttpMessageWriter<MultiValueMap<String, String>>

the fallback writer for form data, null by default

Since
6.0.3

Method Detail

canWriteback to summary
public boolean canWrite(ResolvableType elementType, MediaType mediaType)

Implements org.springframework.http.codec.HttpMessageWriter.canWrite.

Doc from org.springframework.http.codec.HttpMessageWriter.canWrite.

Whether the given object type is supported by this writer.

Parameters
elementType:ResolvableType

the type of object to check

mediaType:MediaType

the media type for the write (possibly null)

Returns:boolean

true if writable, false otherwise

Annotations
@Override
encodePartback to summary
private <T> Flux<DataBuffer> encodePart(byte[] boundary, String name, T value, DataBufferFactory factory)
Annotations
@SuppressWarnings:rawtypes, unchecked
encodePartValuesback to summary
private Flux<DataBuffer> encodePartValues(byte[] boundary, String name, List<?> values, DataBufferFactory bufferFactory)
getFormWriterback to summary
public HttpMessageWriter<MultiValueMap<String, String>> getFormWriter()

Return the configured form writer.

Annotations
@Nullable
Since
5.1.13
getPartWritersback to summary
public List<HttpMessageWriter<?>> getPartWriters()

Return the configured part writers.

Since
5.0.7
initMediaTypesback to summary
private static List<MediaType> initMediaTypes(HttpMessageWriter<?> formWriter)
isMultipartback to summary
private boolean isMultipart(MultiValueMap<String, ?> map, MediaType contentType)
writeback to summary
public Mono<Void> write(Publisher<? extends MultiValueMap<String, ?>> inputStream, ResolvableType elementType, MediaType mediaType, ReactiveHttpOutputMessage outputMessage, Map<String, Object> hints)

Implements org.springframework.http.codec.HttpMessageWriter.write.

Doc from org.springframework.http.codec.HttpMessageWriter.write.

Write a given stream of object to the output message.

Parameters
inputStream:Publisher<? extends MultiValueMap<String, ?>>

the objects to write

elementType:ResolvableType

the type of objects in the stream which must have been previously checked via canWrite(ResolvableType, MediaType)

mediaType:MediaType

the content type for the write (possibly null to indicate that the default content type of the writer must be used)

outputMessage:ReactiveHttpOutputMessage

the message to write to

hints:Map<String, Object>

additional information about how to encode and write

Returns:Mono<Void>

indicates completion or error

Annotations
@Override
writeMultipartback to summary
private Mono<Void> writeMultipart(MultiValueMap<String, ?> map, ReactiveHttpOutputMessage outputMessage, MediaType mediaType, Map<String, Object> hints)
org.springframework.http.codec.multipart back to summary

private Class MultipartHttpMessageWriter.MultipartHttpOutputMessage

extends Object
implements ReactiveHttpOutputMessage
Class Inheritance
  • java.lang.Object
  • org.springframework.http.codec.multipart.MultipartHttpMessageWriter.MultipartHttpOutputMessage
All Implemented Interfaces
org.springframework.http.ReactiveHttpOutputMessage, org.springframework.http.HttpMessage

Field Summary

Modifier and TypeField and Description
private Flux<DataBuffer>
private final DataBufferFactory
private final AtomicBoolean
private final HttpHeaders

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
beforeCommit(Supplier<? extends Mono<Void>>
the action to apply
action
)

Implements org.springframework.http.ReactiveHttpOutputMessage.beforeCommit.

Register an action to apply just before the HttpOutputMessage is committed.
public DataBufferFactory
public Flux<DataBuffer>
public HttpHeaders
getHeaders()

Implements org.springframework.http.HttpMessage.getHeaders.

Return the headers of this message.
public boolean
isCommitted()

Implements org.springframework.http.ReactiveHttpOutputMessage.isCommitted.

Whether the HttpOutputMessage is committed.
public Mono<Void>
setComplete()

Implements org.springframework.http.ReactiveHttpOutputMessage.setComplete.

Indicate that message handling is complete, allowing for any cleanup or end-of-processing tasks to be performed such as applying header changes made via getHeaders() to the underlying HTTP message (if not applied already).
public Mono<Void>
writeAndFlushWith(Publisher<? extends Publisher<? extends DataBuffer>>
the body content publisher
body
)

Implements org.springframework.http.ReactiveHttpOutputMessage.writeAndFlushWith.

Use the given Publisher of Publishers to write the body of the HttpOutputMessage to the underlying HTTP layer, flushing after each Publisher<DataBuffer>.
public Mono<Void>
writeWith(Publisher<? extends DataBuffer>
the body content publisher
body
)

Implements org.springframework.http.ReactiveHttpOutputMessage.writeWith.

Use the given Publisher to write the body of the message to the underlying HTTP layer.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

bodyback to summary
private Flux<DataBuffer> body
Annotations
@Nullable
bufferFactoryback to summary
private final DataBufferFactory bufferFactory
committedback to summary
private final AtomicBoolean committed
headersback to summary
private final HttpHeaders headers

Constructor Detail

MultipartHttpOutputMessageback to summary
public MultipartHttpOutputMessage(DataBufferFactory bufferFactory)

Method Detail

beforeCommitback to summary
public void beforeCommit(Supplier<? extends Mono<Void>> action)

Implements org.springframework.http.ReactiveHttpOutputMessage.beforeCommit.

Doc from org.springframework.http.ReactiveHttpOutputMessage.beforeCommit.

Register an action to apply just before the HttpOutputMessage is committed.

Note

the supplied action must be properly deferred, e.g. via Mono#defer or Mono#fromRunnable, to ensure it's executed in the right order, relative to other actions.

Parameters
action:Supplier<? extends Mono<Void>>

the action to apply

Annotations
@Override
bufferFactoryback to summary
public DataBufferFactory bufferFactory()

Implements org.springframework.http.ReactiveHttpOutputMessage.bufferFactory.

Doc from org.springframework.http.ReactiveHttpOutputMessage.bufferFactory.

Return a DataBufferFactory that can be used to create the body.

Returns:DataBufferFactory

a buffer factory

Annotations
@Override
getBodyback to summary
public Flux<DataBuffer> getBody()
getHeadersback to summary
public HttpHeaders getHeaders()

Implements org.springframework.http.HttpMessage.getHeaders.

Doc from org.springframework.http.HttpMessage.getHeaders.

Return the headers of this message.

Returns:HttpHeaders

a corresponding HttpHeaders object (never null)

Annotations
@Override
isCommittedback to summary
public boolean isCommitted()

Implements org.springframework.http.ReactiveHttpOutputMessage.isCommitted.

Doc from org.springframework.http.ReactiveHttpOutputMessage.isCommitted.

Whether the HttpOutputMessage is committed.

Annotations
@Override
setCompleteback to summary
public Mono<Void> setComplete()

Implements org.springframework.http.ReactiveHttpOutputMessage.setComplete.

Doc from org.springframework.http.ReactiveHttpOutputMessage.setComplete.

Indicate that message handling is complete, allowing for any cleanup or end-of-processing tasks to be performed such as applying header changes made via getHeaders() to the underlying HTTP message (if not applied already).

This method should be automatically invoked at the end of message processing so typically applications should not have to invoke it. If invoked multiple times it should have no side effects.

Returns:Mono<Void>

a Mono that indicates completion or error

Annotations
@Override
writeAndFlushWithback to summary
public Mono<Void> writeAndFlushWith(Publisher<? extends Publisher<? extends DataBuffer>> body)

Implements org.springframework.http.ReactiveHttpOutputMessage.writeAndFlushWith.

Doc from org.springframework.http.ReactiveHttpOutputMessage.writeAndFlushWith.

Use the given Publisher of Publishers to write the body of the HttpOutputMessage to the underlying HTTP layer, flushing after each Publisher<DataBuffer>.

Parameters
body:Publisher<? extends Publisher<? extends DataBuffer>>

the body content publisher

Returns:Mono<Void>

a Mono that indicates completion or error

Annotations
@Override
writeWithback to summary
public Mono<Void> writeWith(Publisher<? extends DataBuffer> body)

Implements org.springframework.http.ReactiveHttpOutputMessage.writeWith.

Doc from org.springframework.http.ReactiveHttpOutputMessage.writeWith.

Use the given Publisher to write the body of the message to the underlying HTTP layer.

Parameters
body:Publisher<? extends DataBuffer>

the body content publisher

Returns:Mono<Void>

a Mono that indicates completion or error

Annotations
@Override