Class FormHttpMessageConverter

  • All Implemented Interfaces:
    HttpMessageConverter<MultiValueMap<String,​?>>
    Direct Known Subclasses:
    AllEncompassingFormHttpMessageConverter, XmlAwareFormHttpMessageConverter

    public class FormHttpMessageConverter
    extends Object
    implements HttpMessageConverter<MultiValueMap<String,​?>>
    Implementation of HttpMessageConverter to read and write 'normal' HTML forms and also to write (but not read) multipart data (e.g. file uploads).

    In other words, this converter can read and write the "application/x-www-form-urlencoded" media type as MultiValueMap<String, String> and it can also write (but not read) the "multipart/form-data" media type as MultiValueMap<String, Object>.

    When writing multipart data, this converter uses other HttpMessageConverters to write the respective MIME parts. By default, basic converters are registered (for Strings and Resources). These can be overridden through the partConverters property.

    For example, the following snippet shows how to submit an HTML form:

     RestTemplate template = new RestTemplate();
     // AllEncompassingFormHttpMessageConverter is configured by default
    
     MultiValueMap<String, String> form = new LinkedMultiValueMap<>();
     form.add("field 1", "value 1");
     form.add("field 2", "value 2");
     form.add("field 2", "value 3");
     template.postForLocation("https://example.com/myForm", form);
     

    The following snippet shows how to do a file upload:

     MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>();
     parts.add("field 1", "value 1");
     parts.add("file", new ClassPathResource("myFile.jpg"));
     template.postForLocation("https://example.com/myFileUpload", parts);
     

    Some methods in this class were inspired by org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity.

    Since:
    3.0
    Author:
    Arjen Poutsma, Rossen Stoyanchev, Juergen Hoeller
    See Also:
    AllEncompassingFormHttpMessageConverter, MultiValueMap