Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -315,6 +316,11 @@ public void setContentType(@Nullable String contentType) {
if (mediaType.getCharset() != null) {
setExplicitCharacterEncoding(mediaType.getCharset().name());
}
else {
if (contentType.equals(MediaType.APPLICATION_JSON_VALUE)) {
this.characterEncoding = StandardCharsets.UTF_8.name();
}
}
}
catch (Exception ex) {
// Try to get charset value anyway
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import org.springframework.http.MediaType;
import org.springframework.web.util.WebUtils;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -360,6 +361,14 @@ void servletWriterAutoFlushedForString() throws IOException {
assertThat(response.getContentAsString()).isEqualTo("X");
}

@Test
void getContentAsStringWhenContentTypeIsApplicationJsonShouldUseUtf8() throws IOException {
String content = "{\"value\": \"테스트\"}";
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
response.getWriter().write(content);
assertThat(response.getContentAsString()).isEqualTo(content);
}

@Test
void sendRedirect() throws IOException {
String redirectUrl = "/redirect";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -315,6 +316,11 @@ public void setContentType(@Nullable String contentType) {
if (mediaType.getCharset() != null) {
setExplicitCharacterEncoding(mediaType.getCharset().name());
}
else {
if (contentType.equals(MediaType.APPLICATION_JSON_VALUE)) {
this.characterEncoding = StandardCharsets.UTF_8.name();
}
}
}
catch (Exception ex) {
// Try to get charset value anyway
Expand Down