Use UTF-8 for application/*+json

This commit makes sure that the StringHttpMessageConverter reads
input with "application/*+json" as Content-Type with the UTF-8
character set.

Closes gh-25328
This commit is contained in:
Arjen Poutsma
2020-07-01 10:46:46 +02:00
parent b2a4d1c5b1
commit 5b94d9b00b
2 changed files with 32 additions and 3 deletions

View File

@@ -43,6 +43,8 @@ import org.springframework.util.StreamUtils;
*/
public class StringHttpMessageConverter extends AbstractHttpMessageConverter<String> {
private static final MediaType APPLICATION_PLUS_JSON = new MediaType("application", "*+json");
/**
* The default charset used by the converter.
*/
@@ -104,7 +106,9 @@ public class StringHttpMessageConverter extends AbstractHttpMessageConverter<Str
@Override
protected void addDefaultHeaders(HttpHeaders headers, String s, @Nullable MediaType type) throws IOException {
if (headers.getContentType() == null ) {
if (type != null && type.isConcrete() && type.isCompatibleWith(MediaType.APPLICATION_JSON)) {
if (type != null && type.isConcrete() &&
(type.isCompatibleWith(MediaType.APPLICATION_JSON) ||
type.isCompatibleWith(APPLICATION_PLUS_JSON))) {
// Prevent charset parameter for JSON..
headers.setContentType(type);
}
@@ -142,7 +146,9 @@ public class StringHttpMessageConverter extends AbstractHttpMessageConverter<Str
if (contentType != null && contentType.getCharset() != null) {
return contentType.getCharset();
}
else if (contentType != null && contentType.isCompatibleWith(MediaType.APPLICATION_JSON)) {
else if (contentType != null &&
(contentType.isCompatibleWith(MediaType.APPLICATION_JSON) ||
contentType.isCompatibleWith(APPLICATION_PLUS_JSON))) {
// Matching to AbstractJackson2HttpMessageConverter#DEFAULT_CHARSET
return StandardCharsets.UTF_8;
}