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

public Class FormHttpMessageWriter

extends LoggingCodecSupport
implements HttpMessageWriter<MultiValueMap<String, String>>
Class Inheritance
All Implemented Interfaces
org.springframework.http.codec.HttpMessageWriter
Imports
java.net.URLEncoder, java.nio.ByteBuffer, java.nio.charset.Charset, .StandardCharsets, java.util.Collections, .List, .Map, org.reactivestreams.Publisher, reactor.core.publisher.Mono, org.springframework.core.ResolvableType, org.springframework.core.codec.Hints, org.springframework.core.io.buffer.DataBuffer, org.springframework.core.log.LogFormatUtils, org.springframework.http.MediaType, .ReactiveHttpOutputMessage, org.springframework.lang.Nullable, org.springframework.util.Assert, .MultiValueMap

HttpMessageWriter for writing a MultiValueMap<String, String> as HTML form data, i.e. "application/x-www-form-urlencoded", to the body of a request.

Note that unless the media type is explicitly set to MediaType#APPLICATION_FORM_URLENCODED, the canWrite method will need generic type information to confirm the target map has String values. This is because a MultiValueMap with non-String values can be used to write multipart requests.

To support both form data and multipart requests, consider using org.springframework.http.codec.multipart.MultipartHttpMessageWriter configured with this writer as the fallback for writing plain form data.

Authors
Sebastien Deleuze, Rossen Stoyanchev
Since
5.0
See Also
org.springframework.http.codec.multipart.MultipartHttpMessageWriter

Field Summary

Modifier and TypeField and Description
public static final Charset
DEFAULT_CHARSET

The default charset used by the writer.

private static final MediaType
private Charset
private static final List<MediaType>
private static final ResolvableType
Inherited from org.springframework.http.codec.LoggingCodecSupport:
logger

Constructor Summary

AccessConstructor and Description
public

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.

public Charset
getDefaultCharset()

Return the configured default charset.

protected MediaType
public List<MediaType>
getWritableMediaTypes()

Implements org.springframework.http.codec.HttpMessageWriter.getWritableMediaTypes.

Return the list of media types supported by this Writer.

private void
protected String
public void
setDefaultCharset(Charset charset)

Set the default character set to use for writing form data when the response Content-Type header does not explicitly specify it.

public Mono<Void>
write(Publisher<? extends MultiValueMap<String, 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
message
,
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.

Inherited from org.springframework.http.codec.LoggingCodecSupport:
isEnableLoggingRequestDetailssetEnableLoggingRequestDetails

Field Detail

DEFAULT_CHARSETback to summary
public static final Charset DEFAULT_CHARSET

The default charset used by the writer.

DEFAULT_FORM_DATA_MEDIA_TYPEback to summary
private static final MediaType DEFAULT_FORM_DATA_MEDIA_TYPE
defaultCharsetback to summary
private Charset defaultCharset
MEDIA_TYPESback to summary
private static final List<MediaType> MEDIA_TYPES
MULTIVALUE_TYPEback to summary
private static final ResolvableType MULTIVALUE_TYPE

Constructor Detail

FormHttpMessageWriterback to summary
public FormHttpMessageWriter()

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
getDefaultCharsetback to summary
public Charset getDefaultCharset()

Return the configured default charset.

getMediaTypeback to summary
protected MediaType getMediaType(MediaType mediaType)
getWritableMediaTypesback to summary
public List<MediaType> getWritableMediaTypes()

Implements org.springframework.http.codec.HttpMessageWriter.getWritableMediaTypes.

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

Return the list of media types supported by this Writer. The list may not apply to every possible target element type and calls to this method should typically be guarded via canWrite(elementType, null). The list may also exclude media types supported only for a specific element type. Alternatively, use getWritableMediaTypes(ResolvableType) for a more precise list.

Returns:List<MediaType>

the general list of supported media types

Annotations
@Override
logFormDataback to summary
private void logFormData(MultiValueMap<String, String> form, Map<String, Object> hints)
serializeFormback to summary
protected String serializeForm(MultiValueMap<String, String> formData, Charset charset)
setDefaultCharsetback to summary
public void setDefaultCharset(Charset charset)

Set the default character set to use for writing form data when the response Content-Type header does not explicitly specify it.

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

writeback to summary
public Mono<Void> write(Publisher<? extends MultiValueMap<String, String>> inputStream, ResolvableType elementType, MediaType mediaType, ReactiveHttpOutputMessage message, 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, 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)

message: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