File tree 3 files changed +21
-0
lines changed
main/java/org/springframework/mock/web
test/java/org/springframework/mock/web
spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet
3 files changed +21
-0
lines changed Original file line number Diff line number Diff line change 24
24
import java .io .UnsupportedEncodingException ;
25
25
import java .io .Writer ;
26
26
import java .nio .charset .Charset ;
27
+ import java .nio .charset .StandardCharsets ;
27
28
import java .text .DateFormat ;
28
29
import java .text .ParseException ;
29
30
import java .text .SimpleDateFormat ;
@@ -315,6 +316,11 @@ public void setContentType(@Nullable String contentType) {
315
316
if (mediaType .getCharset () != null ) {
316
317
setExplicitCharacterEncoding (mediaType .getCharset ().name ());
317
318
}
319
+ else {
320
+ if (contentType .equals (MediaType .APPLICATION_JSON_VALUE )) {
321
+ this .characterEncoding = StandardCharsets .UTF_8 .name ();
322
+ }
323
+ }
318
324
}
319
325
catch (Exception ex ) {
320
326
// Try to get charset value anyway
Original file line number Diff line number Diff line change 30
30
import org .junit .jupiter .params .ParameterizedTest ;
31
31
import org .junit .jupiter .params .provider .ValueSource ;
32
32
33
+ import org .springframework .http .MediaType ;
33
34
import org .springframework .web .util .WebUtils ;
34
35
35
36
import static org .assertj .core .api .Assertions .assertThat ;
@@ -360,6 +361,14 @@ void servletWriterAutoFlushedForString() throws IOException {
360
361
assertThat (response .getContentAsString ()).isEqualTo ("X" );
361
362
}
362
363
364
+ @ Test
365
+ void getContentAsStringWhenContentTypeIsApplicationJsonShouldUseUtf8 () throws IOException {
366
+ String content = "{\" value\" : \" 테스트\" }" ;
367
+ response .setContentType (MediaType .APPLICATION_JSON_VALUE );
368
+ response .getWriter ().write (content );
369
+ assertThat (response .getContentAsString ()).isEqualTo (content );
370
+ }
371
+
363
372
@ Test
364
373
void sendRedirect () throws IOException {
365
374
String redirectUrl = "/redirect" ;
Original file line number Diff line number Diff line change 24
24
import java .io .UnsupportedEncodingException ;
25
25
import java .io .Writer ;
26
26
import java .nio .charset .Charset ;
27
+ import java .nio .charset .StandardCharsets ;
27
28
import java .text .DateFormat ;
28
29
import java .text .ParseException ;
29
30
import java .text .SimpleDateFormat ;
@@ -315,6 +316,11 @@ public void setContentType(@Nullable String contentType) {
315
316
if (mediaType .getCharset () != null ) {
316
317
setExplicitCharacterEncoding (mediaType .getCharset ().name ());
317
318
}
319
+ else {
320
+ if (contentType .equals (MediaType .APPLICATION_JSON_VALUE )) {
321
+ this .characterEncoding = StandardCharsets .UTF_8 .name ();
322
+ }
323
+ }
318
324
}
319
325
catch (Exception ex ) {
320
326
// Try to get charset value anyway
You can’t perform that action at this time.
0 commit comments