Class FormHttpMessageConverter

java.lang.Object
org.springframework.http.converter.FormHttpMessageConverter
All Implemented Interfaces:
HttpMessageConverter<Object>, SmartHttpMessageConverter<Object>

public class FormHttpMessageConverter extends Object implements 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:
  • MultiValueMap
  • Field Details

    • DEFAULT_CHARSET

      public static final Charset DEFAULT_CHARSET
      The default charset used by the converter.
  • Constructor Details

    • FormHttpMessageConverter

      public FormHttpMessageConverter()
  • Method Details

    • getSupportedMediaTypes

      public List<MediaType> 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 via canWrite(clazz, null. The list may also exclude MIME types supported only for a specific class. Alternatively, use HttpMessageConverter.getSupportedMediaTypes(Class) for a more precise list.
      Specified by:
      getSupportedMediaTypes in interface HttpMessageConverter<Object>
      Returns:
      the list of supported media types
    • setCharset

      public void setCharset(@Nullable Charset charset)
      Set the default character set to use for reading and writing form data when the request or response Content-Type header does not explicitly specify it.

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

    • canRead

      public boolean canRead(org.springframework.core.ResolvableType type, @Nullable MediaType mediaType)
      Description copied from interface: SmartHttpMessageConverter
      Indicates whether the given type can be read by this converter. This method should perform the same checks as HttpMessageConverter.canRead(Class, MediaType) with additional ones related to the generic type.
      Specified by:
      canRead in interface SmartHttpMessageConverter<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 be null if not specified. Typically, the value of a Content-Type header.
      Returns:
      true if readable; false otherwise
    • canWrite

      public boolean canWrite(org.springframework.core.ResolvableType targetType, Class<?> valueClass, @Nullable MediaType mediaType)
      Description copied from interface: SmartHttpMessageConverter
      Indicates 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:
      canWrite in interface SmartHttpMessageConverter<Object>
      Parameters:
      targetType - the (potentially generic) target type to test for writability (can be ResolvableType.NONE if 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 writability
      mediaType - the media type to write (can be null if not specified); typically the value of an Accept header.
      Returns:
      true if writable; false otherwise
    • read

      public Object read(org.springframework.core.ResolvableType type, HttpInputMessage inputMessage, @Nullable Map<String,Object> hints) throws IOException, HttpMessageNotReadableException
      Description copied from interface: SmartHttpMessageConverter
      Read an object of the given type from the given input message, and returns it.
      Specified by:
      read in interface SmartHttpMessageConverter<Object>
      Parameters:
      type - the (potentially generic) type of object to return. This type must have previously been passed to the canRead method of this interface, which must have returned true. 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 from
      hints - additional information about how to encode
      Returns:
      the converted object
      Throws:
      IOException - in case of I/O errors
      HttpMessageNotReadableException - in case of conversion errors
    • write

      public void write(Object data, org.springframework.core.ResolvableType type, @Nullable MediaType contentType, HttpOutputMessage outputMessage, @Nullable Map<String,Object> hints) throws IOException, HttpMessageNotWritableException
      Description copied from interface: SmartHttpMessageConverter
      Write a given object to the given output message.
      Specified by:
      write in interface SmartHttpMessageConverter<Object>
      Parameters:
      data - the object to write to the output message. The type of this object must have previously been passed to the canWrite method of this interface, which must have returned true.
      type - the (potentially generic) type of object to write. This type must have previously been passed to the canWrite method of this interface, which must have returned true. Can be ResolvableType.NONE if 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 be null to indicate that the default content type of the converter must be used. If not null, this media type must have previously been passed to the canWrite method of this interface, which must have returned true.
      outputMessage - the message to write to
      hints - additional information about how to encode
      Throws:
      IOException - in case of I/O errors
      HttpMessageNotWritableException - in case of conversion errors
    • getFormContentType

      protected MediaType getFormContentType(@Nullable MediaType contentType)
      Return the content type used to write forms, either the given content type or otherwise application/x-www-form-urlencoded.
      Parameters:
      contentType - the content type passed to write(Object, ResolvableType, MediaType, HttpOutputMessage, Map), or null
      Returns:
      the content type to use
      Since:
      5.2.2
    • serializeForm

      protected String serializeForm(Map<String,String> formData, Charset charset)
    • serializeForm

      protected String serializeForm(org.springframework.util.MultiValueMap<String,String> formData, Charset charset)