org.springframework.http.codec.multipart

Class MultipartHttpMessageWriter

  • All Implemented Interfaces:
    HttpMessageWriter<MultiValueMap<java.lang.String,?>>


    public class MultipartHttpMessageWriter
    extends LoggingCodecSupport
    implements HttpMessageWriter<MultiValueMap<java.lang.String,?>>
    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.

    Since:
    5.0
    Author:
    Sebastien Deleuze, Rossen Stoyanchev
    See Also:
    FormHttpMessageWriter
    • Field Detail

      • DEFAULT_CHARSET

        public static final java.nio.charset.Charset DEFAULT_CHARSET
        THe default charset used by the writer.
    • Constructor Detail

      • MultipartHttpMessageWriter

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

        public MultipartHttpMessageWriter(java.util.List<HttpMessageWriter<?>> partWriters)
        Constructor with explicit list of writers for serializing parts.
      • MultipartHttpMessageWriter

        public MultipartHttpMessageWriter(java.util.List<HttpMessageWriter<?>> partWriters,
                                          @Nullable
                                          HttpMessageWriter<MultiValueMap<java.lang.String,java.lang.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 - the writers for serializing parts
        formWriter - the fallback writer for form data, null by default
    • Method Detail

      • getPartWriters

        public java.util.List<HttpMessageWriter<?>> getPartWriters()
        Return the configured part writers.
        Since:
        5.0.7
      • setCharset

        public void setCharset(java.nio.charset.Charset charset)
        Set the character set to use for part headers such as "Content-Disposition" (and its filename parameter).

        By default this is set to "UTF-8".

      • getCharset

        public java.nio.charset.Charset getCharset()
        Return the configured charset for part headers.
      • canWrite

        public boolean canWrite(ResolvableType elementType,
                                @Nullable
                                MediaType mediaType)
        Description copied from interface: HttpMessageWriter
        Whether the given object type is supported by this writer.
        Specified by:
        canWrite in interface HttpMessageWriter<MultiValueMap<java.lang.String,?>>
        Parameters:
        elementType - the type of object to check
        mediaType - the media type for the write, possibly null
        Returns:
        true if writable, false otherwise
      • write

        public reactor.core.publisher.Mono<java.lang.Void> write(org.reactivestreams.Publisher<? extends MultiValueMap<java.lang.String,?>> inputStream,
                                                                 ResolvableType elementType,
                                                                 @Nullable
                                                                 MediaType mediaType,
                                                                 ReactiveHttpOutputMessage outputMessage,
                                                                 java.util.Map<java.lang.String,java.lang.Object> hints)
        Description copied from interface: HttpMessageWriter
        Write an given stream of object to the output message.
        Specified by:
        write in interface HttpMessageWriter<MultiValueMap<java.lang.String,?>>
        Parameters:
        inputStream - the objects to write
        elementType - the type of objects in the stream which must have been previously checked via HttpMessageWriter.canWrite(ResolvableType, MediaType)
        mediaType - the content type for the write, possibly null to indicate that the default content type of the writer must be used.
        outputMessage - the message to write to
        hints - additional information about how to encode and write
        Returns:
        indicates completion or error