Commit
This commit restores the interpretation of JSON as UTF-8 by default that was removed in #bc205e0 and also ensures a charset is not appended automatically to "application/json". Closes gh-24123
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -100,6 +100,18 @@ protected Long getContentLength(String str, @Nullable MediaType contentType) { | |
return (long) str.getBytes(charset).length; | ||
} | ||
|
||
|
||
@Override | ||
protected void addDefaultHeaders(HttpHeaders headers, String s, @Nullable MediaType mediaType) throws IOException { | ||
if (headers.getContentType() == null ) { | ||
if (mediaType != null && mediaType.isCompatibleWith(MediaType.APPLICATION_JSON)) { | ||
// Prevent charset parameter for JSON.. | ||
headers.setContentType(mediaType); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
rstoyanchev
Author
Contributor
|
||
} | ||
} | ||
super.addDefaultHeaders(headers, s, mediaType); | ||
} | ||
|
||
@Override | ||
protected void writeInternal(String str, HttpOutputMessage outputMessage) throws IOException { | ||
HttpHeaders headers = outputMessage.getHeaders(); | ||
|
@@ -130,6 +142,10 @@ private Charset getContentTypeCharset(@Nullable MediaType contentType) { | |
if (contentType != null && contentType.getCharset() != null) { | ||
return contentType.getCharset(); | ||
} | ||
else if (contentType != null && contentType.isCompatibleWith(MediaType.APPLICATION_JSON)) { | ||
// Matching to AbstractJackson2HttpMessageConverter#DEFAULT_CHARSET | ||
return StandardCharsets.UTF_8; | ||
} | ||
else { | ||
Charset charset = getDefaultCharset(); | ||
Assert.state(charset != null, "No default charset"); | ||
|
I have just started to fail with this change.
My content type candidate is like this one:
So,
MediaType.ALL
is really compatible withMediaType.APPLICATION_JSON
, but it is wrong value for theContent-Type
header:Any clues what to use instead of
MediaType.ALL
when noAccept
header in the request?Or maybe this fix should be improved to skip
MediaType.ALL
as it is done in the super class:Thank you!