Class FormHttpMessageConverter
java.lang.Object
org.springframework.http.converter.FormHttpMessageConverter
- All Implemented Interfaces:
HttpMessageConverter<Object>, SmartHttpMessageConverter<Object>
Implementation of
HttpMessageConverter to read and
write URL encoded forms. For multipart support, see the
MultipartHttpMessageConverter.
This converter can read and write the
"application/x-www-form-urlencoded" media type as
MultiValueMap<String, String>.
Examples
The following snippet shows how to submit an HTML form using the
"application/x-www-form-urlencoded" content type.
RestClient restClient = RestClient.create();
MultiValueMap<String, String> form = new LinkedMultiValueMap<>();
form.add("field 1", "value 1");
form.add("field 2", "value 2");
form.add("field 2", "value 3");
form.add("field 3", 4);
ResponseEntity<Void> response = restClient.post()
.uri("https://example.com/myForm")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.body(form)
.retrieve()
.toBodilessEntity();- Since:
- 3.0
- Author:
- Arjen Poutsma, Rossen Stoyanchev, Juergen Hoeller, Sam Brannen, Brian Clozel
- See Also:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleancanRead(ResolvableType type, @Nullable MediaType mediaType) Indicates whether the given type can be read by this converter.booleancanWrite(ResolvableType targetType, Class<?> valueClass, @Nullable MediaType mediaType) Indicates whether the given class can be written by this converter.protected MediaTypegetFormContentType(@Nullable MediaType contentType) Return the content type used to write forms, either the given content type or otherwiseapplication/x-www-form-urlencoded.Return the list of media types supported by this converter.read(ResolvableType type, HttpInputMessage inputMessage, @Nullable Map<String, Object> hints) Read an object of the given type from the given input message, and returns it.protected StringserializeForm(Map<String, String> formData, Charset charset) protected StringserializeForm(MultiValueMap<String, String> formData, Charset charset) voidsetCharset(@Nullable Charset charset) Set the default character set to use for reading and writing form data when the request or responseContent-Typeheader does not explicitly specify it.voidwrite(Object data, ResolvableType type, @Nullable MediaType contentType, HttpOutputMessage outputMessage, @Nullable Map<String, Object> hints) Write a given object to the given output message.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface HttpMessageConverter
canWriteRepeatedly, getSupportedMediaTypesMethods inherited from interface SmartHttpMessageConverter
canRead, canWrite, read, write
-
Field Details
-
DEFAULT_CHARSET
The default charset used by the converter.
-
-
Constructor Details
-
FormHttpMessageConverter
public FormHttpMessageConverter()
-
-
Method Details
-
getSupportedMediaTypes
Return the list of media types supported by this converter. The list may not apply to every possible target element type and calls to this method should typically be guarded viacanWrite(clazz, null. The list may also exclude MIME types supported only for a specific class. Alternatively, useHttpMessageConverter.getSupportedMediaTypes(Class)for a more precise list.- Specified by:
getSupportedMediaTypesin interfaceHttpMessageConverter<Object>- Returns:
- the list of supported media types
-
setCharset
-
canRead
Description copied from interface:SmartHttpMessageConverterIndicates whether the given type can be read by this converter. This method should perform the same checks asHttpMessageConverter.canRead(Class, MediaType)with additional ones related to the generic type.- Specified by:
canReadin interfaceSmartHttpMessageConverter<Object>- Parameters:
type- the (potentially generic) type to test for readability. The type source may be used for retrieving additional information (the related method signature for example) when relevant.mediaType- the media type to read, can benullif not specified. Typically, the value of aContent-Typeheader.- Returns:
trueif readable;falseotherwise
-
canWrite
public boolean canWrite(ResolvableType targetType, Class<?> valueClass, @Nullable MediaType mediaType) Description copied from interface:SmartHttpMessageConverterIndicates whether the given class can be written by this converter.This method should perform the same checks as
HttpMessageConverter.canWrite(Class, MediaType)with additional ones related to the generic type.- Specified by:
canWritein interfaceSmartHttpMessageConverter<Object>- Parameters:
targetType- the (potentially generic) target type to test for writability (can beResolvableType.NONEif not specified). The type source may be used for retrieving additional information (the related method signature for example) when relevant.valueClass- the source object class to test for writabilitymediaType- the media type to write (can benullif not specified); typically the value of anAcceptheader.- Returns:
trueif writable;falseotherwise
-
read
public Object read(ResolvableType type, HttpInputMessage inputMessage, @Nullable Map<String, Object> hints) throws IOException, HttpMessageNotReadableExceptionDescription copied from interface:SmartHttpMessageConverterRead an object of the given type from the given input message, and returns it.- Specified by:
readin interfaceSmartHttpMessageConverter<Object>- Parameters:
type- the (potentially generic) type of object to return. This type must have previously been passed to thecanReadmethod of this interface, which must have returnedtrue. The type source may be used for retrieving additional information (the related method signature for example) when relevant.inputMessage- the HTTP input message to read fromhints- additional information about how to encode- Returns:
- the converted object
- Throws:
IOException- in case of I/O errorsHttpMessageNotReadableException- in case of conversion errors
-
write
public void write(Object data, ResolvableType type, @Nullable MediaType contentType, HttpOutputMessage outputMessage, @Nullable Map<String, Object> hints) throws IOException, HttpMessageNotWritableExceptionDescription copied from interface:SmartHttpMessageConverterWrite a given object to the given output message.- Specified by:
writein interfaceSmartHttpMessageConverter<Object>- Parameters:
data- the object to write to the output message. The type of this object must have previously been passed to thecanWritemethod of this interface, which must have returnedtrue.type- the (potentially generic) type of object to write. This type must have previously been passed to thecanWritemethod of this interface, which must have returnedtrue. Can beResolvableType.NONEif not specified. The type source may be used for retrieving additional information (the related method signature for example) when relevant.contentType- the content type to use when writing. May benullto indicate that the default content type of the converter must be used. If notnull, this media type must have previously been passed to thecanWritemethod of this interface, which must have returnedtrue.outputMessage- the message to write tohints- additional information about how to encode- Throws:
IOException- in case of I/O errorsHttpMessageNotWritableException- in case of conversion errors
-
getFormContentType
Return the content type used to write forms, either the given content type or otherwiseapplication/x-www-form-urlencoded.- Parameters:
contentType- the content type passed towrite(Object, ResolvableType, MediaType, HttpOutputMessage, Map), ornull- Returns:
- the content type to use
- Since:
- 5.2.2
-
serializeForm
-
serializeForm
-