Class MultipartHttpMessageWriter
- java.lang.Object
- org.springframework.http.codec.LoggingCodecSupport
- org.springframework.http.codec.multipart.MultipartHttpMessageWriter
- All Implemented Interfaces:
HttpMessageWriter<MultiValueMap<String,?>>
public class MultipartHttpMessageWriter extends LoggingCodecSupport implements HttpMessageWriter<MultiValueMap<String,?>>
HttpMessageWriterfor writing aMultiValueMap<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
StringandResourceparts are supported but you can configure others through a constructor argument.This writer can be configured with a
FormHttpMessageWriterto 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 theMediaTypeis 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 Summary
Fields Modifier and Type Field Description static CharsetDEFAULT_CHARSETTHe default charset used by the writer.Fields inherited from class org.springframework.http.codec.LoggingCodecSupport
logger
Constructor Summary
Constructors Constructor Description MultipartHttpMessageWriter()Constructor with a default list of part writers (String and Resource).MultipartHttpMessageWriter(List<HttpMessageWriter<?>> partWriters)Constructor with explicit list of writers for serializing parts.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.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleancanWrite(ResolvableType elementType, MediaType mediaType)Whether the given object type is supported by this writer.protected byte[]generateMultipartBoundary()Generate a multipart boundary.CharsetgetCharset()Return the configured charset for part headers.HttpMessageWriter<MultiValueMap<String,String>>getFormWriter()Return the configured form writer.List<HttpMessageWriter<?>>getPartWriters()Return the configured part writers.List<MediaType>getWritableMediaTypes()Return theMediaType's that this writer supports.voidsetCharset(Charset charset)Set the character set to use for part headers such as "Content-Disposition" (and its filename parameter).reactor.core.publisher.Mono<Void>write(org.reactivestreams.Publisher<? extends MultiValueMap<String,?>> inputStream, ResolvableType elementType, MediaType mediaType, ReactiveHttpOutputMessage outputMessage, Map<String,Object> hints)Write an given stream of object to the output message.Methods inherited from class org.springframework.http.codec.LoggingCodecSupport
isEnableLoggingRequestDetails, setEnableLoggingRequestDetails
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.springframework.http.codec.HttpMessageWriter
write
Field Detail
DEFAULT_CHARSET
public static final 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(List<HttpMessageWriter<?>> partWriters)
Constructor with explicit list of writers for serializing parts.
MultipartHttpMessageWriter
public MultipartHttpMessageWriter(List<HttpMessageWriter<?>> partWriters, @Nullable 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- the writers for serializing partsformWriter- the fallback writer for form data,nullby default
Method Detail
getPartWriters
public List<HttpMessageWriter<?>> getPartWriters()
Return the configured part writers.- Since:
- 5.0.7
getFormWriter
@Nullable public HttpMessageWriter<MultiValueMap<String,String>> getFormWriter()
Return the configured form writer.- Since:
- 5.1.13
setCharset
public void setCharset(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 Charset getCharset()
Return the configured charset for part headers.
getWritableMediaTypes
public List<MediaType> getWritableMediaTypes()
Description copied from interface:HttpMessageWriterReturn theMediaType's that this writer supports.- Specified by:
getWritableMediaTypesin interfaceHttpMessageWriter<MultiValueMap<String,?>>
canWrite
public boolean canWrite(ResolvableType elementType, @Nullable MediaType mediaType)
Description copied from interface:HttpMessageWriterWhether the given object type is supported by this writer.- Specified by:
canWritein interfaceHttpMessageWriter<MultiValueMap<String,?>>- Parameters:
elementType- the type of object to checkmediaType- the media type for the write (possiblynull)- Returns:
trueif writable,falseotherwise
write
public reactor.core.publisher.Mono<Void> write(org.reactivestreams.Publisher<? extends MultiValueMap<String,?>> inputStream, ResolvableType elementType, @Nullable MediaType mediaType, ReactiveHttpOutputMessage outputMessage, Map<String,Object> hints)
Description copied from interface:HttpMessageWriterWrite an given stream of object to the output message.- Specified by:
writein interfaceHttpMessageWriter<MultiValueMap<String,?>>- Parameters:
inputStream- the objects to writeelementType- the type of objects in the stream which must have been previously checked viaHttpMessageWriter.canWrite(ResolvableType, MediaType)mediaType- the content type for the write (possiblynullto indicate that the default content type of the writer must be used)outputMessage- the message to write tohints- additional information about how to encode and write- Returns:
- indicates completion or error
generateMultipartBoundary
protected byte[] generateMultipartBoundary()
Generate a multipart boundary.By default delegates to
MimeTypeUtils.generateMultipartBoundary().