52
52
* <p>In other words, this converter can read and write the
53
53
* {@code "application/x-www-form-urlencoded"} media type as
54
54
* {@link MultiValueMap MultiValueMap<String, String>}, and it can also
55
- * write (but not read) the {@code "multipart/form-data"} media type as
55
+ * write (but not read) the {@code "multipart/form-data"} and
56
+ * {@code "multipart/mixed"} media types as
56
57
* {@link MultiValueMap MultiValueMap<String, Object>}.
57
58
*
58
59
* <h3>Multipart Data</h3>
63
64
* {@code "multipart/mixed"} and {@code "multipart/related"}, as long as the
64
65
* multipart subtype is registered as a {@linkplain #getSupportedMediaTypes
65
66
* supported media type} <em>and</em> the desired multipart subtype is specified
66
- * as the content type when {@linkplain #write writing} the multipart data.
67
+ * as the content type when {@linkplain #write writing} the multipart data. Note
68
+ * that {@code "multipart/mixed"} is registered as a supported media type by
69
+ * default.
67
70
*
68
71
* <p>When writing multipart data, this converter uses other
69
72
* {@link HttpMessageConverter HttpMessageConverters} to write the respective
85
88
* form.add("field 2", "value 2");
86
89
* form.add("field 2", "value 3");
87
90
* form.add("field 3", 4); // non-String form values supported as of 5.1.4
88
- * restTemplate.postForLocation("https://example.com/myForm", form);
89
- * </pre>
91
+ *
92
+ * restTemplate.postForLocation("https://example.com/myForm", form); </pre>
90
93
*
91
94
* <p>The following snippet shows how to do a file upload using the
92
95
* {@code "multipart/form-data"} content type.
95
98
* MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>();
96
99
* parts.add("field 1", "value 1");
97
100
* parts.add("file", new ClassPathResource("myFile.jpg"));
98
- * restTemplate.postForLocation("https://example.com/myFileUpload", parts);
99
- * </pre>
101
+ *
102
+ * restTemplate.postForLocation("https://example.com/myFileUpload", parts); </pre>
100
103
*
101
104
* <p>The following snippet shows how to do a file upload using the
102
105
* {@code "multipart/mixed"} content type.
103
106
*
104
107
* <pre class="code">
105
- * MediaType multipartMixed = new MediaType("multipart", "mixed");
108
+ * MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>();
109
+ * parts.add("field 1", "value 1");
110
+ * parts.add("file", new ClassPathResource("myFile.jpg"));
111
+ *
112
+ * HttpHeaders requestHeaders = new HttpHeaders();
113
+ * requestHeaders.setContentType(MediaType.MULTIPART_MIXED);
114
+ *
115
+ * restTemplate.postForLocation("https://example.com/myFileUpload",
116
+ * new HttpEntity<>(parts, requestHeaders));</pre>
117
+ *
118
+ * <p>The following snippet shows how to do a file upload using the
119
+ * {@code "multipart/related"} content type.
120
+ *
121
+ * <pre class="code">
122
+ * MediaType multipartRelated = new MediaType("multipart", "related");
106
123
*
107
124
* restTemplate.getMessageConverters().stream()
108
125
* .filter(FormHttpMessageConverter.class::isInstance)
109
126
* .map(FormHttpMessageConverter.class::cast)
110
127
* .findFirst()
111
128
* .orElseThrow(() -> new IllegalStateException("Failed to find FormHttpMessageConverter"))
112
- * .addSupportedMediaTypes(multipartMixed );
129
+ * .addSupportedMediaTypes(multipartRelated );
113
130
*
114
131
* MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>();
115
132
* parts.add("field 1", "value 1");
116
133
* parts.add("file", new ClassPathResource("myFile.jpg"));
117
134
*
118
135
* HttpHeaders requestHeaders = new HttpHeaders();
119
- * requestHeaders.setContentType(multipartMixed);
120
- * HttpEntity<MultiValueMap<String, Object>> requestEntity =
121
- * new HttpEntity<>(parts, requestHeaders);
136
+ * requestHeaders.setContentType(multipartRelated);
122
137
*
123
- * restTemplate.postForLocation("https://example.com/myFileUpload", requestEntity);
124
- * </pre>
138
+ * restTemplate.postForLocation("https://example.com/myFileUpload",
139
+ * new HttpEntity<>(parts, requestHeaders)); </pre>
125
140
*
126
141
* <h3>Miscellaneous</h3>
127
142
*
138
153
*/
139
154
public class FormHttpMessageConverter implements HttpMessageConverter <MultiValueMap <String , ?>> {
140
155
141
- private static final MediaType MULTIPART_ALL = new MediaType ("multipart" , "*" );
142
-
143
156
/**
144
157
* The default charset used by the converter.
145
158
*/
146
159
public static final Charset DEFAULT_CHARSET = StandardCharsets .UTF_8 ;
147
160
161
+ static final MediaType MULTIPART_ALL = new MediaType ("multipart" , "*" );
162
+
148
163
private static final MediaType DEFAULT_FORM_DATA_MEDIA_TYPE =
149
164
new MediaType (MediaType .APPLICATION_FORM_URLENCODED , DEFAULT_CHARSET );
150
165
@@ -162,6 +177,7 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
162
177
public FormHttpMessageConverter () {
163
178
this .supportedMediaTypes .add (MediaType .APPLICATION_FORM_URLENCODED );
164
179
this .supportedMediaTypes .add (MediaType .MULTIPART_FORM_DATA );
180
+ this .supportedMediaTypes .add (MediaType .MULTIPART_MIXED );
165
181
166
182
this .partConverters .add (new ByteArrayHttpMessageConverter ());
167
183
this .partConverters .add (new StringHttpMessageConverter ());
0 commit comments