Browse Source

Expose method to determine form content type

This commit exposes the method that returns the media type used to write
forms. By default, it includes the charset in the content type, which
can cause issues with certain consumers. This commit changes the method
from a private to a protected method, so that users can override the
default behavior.

Closes: gh-22971
pull/24086/head
Arjen Poutsma 6 years ago
parent
commit
ddb38eefee
  1. 24
      spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java

24
spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java

@ -384,7 +384,7 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue @@ -384,7 +384,7 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
private void writeForm(MultiValueMap<String, Object> formData, @Nullable MediaType contentType,
HttpOutputMessage outputMessage) throws IOException {
contentType = getMediaType(contentType);
contentType = getFormContentType(contentType);
outputMessage.getHeaders().setContentType(contentType);
Charset charset = contentType.getCharset();
@ -402,15 +402,27 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue @@ -402,15 +402,27 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
}
}
private MediaType getMediaType(@Nullable MediaType mediaType) {
if (mediaType == null) {
/**
* Return the content type used to write forms, given the preferred content type.
* By default, this method returns the given content type, but adds the
* {@linkplain #setCharset(Charset) charset} if it does not have one.
* If {@code contentType} is {@code null},
* {@code application/x-www-form-urlencoded; charset=UTF-8} is returned.
*
* <p>Subclasses can override this method to change this behavior.
* @param contentType the preferred content type, can be {@code null}
* @return the content type to be used
* @since 5.2.2
*/
protected MediaType getFormContentType(@Nullable MediaType contentType) {
if (contentType == null) {
return DEFAULT_FORM_DATA_MEDIA_TYPE;
}
else if (mediaType.getCharset() == null) {
return new MediaType(mediaType, this.charset);
else if (contentType.getCharset() == null) {
return new MediaType(contentType, this.charset);
}
else {
return mediaType;
return contentType;
}
}

Loading…
Cancel
Save