Skip to content

Commit 27e87e5

Browse files
committed
Document why "charset=UTF-8" is specified for JSON
Issue: SPR-14715
1 parent 967ea15 commit 27e87e5

File tree

4 files changed

+34
-6
lines changed

4 files changed

+34
-6
lines changed

spring-web/src/main/java/org/springframework/http/MediaType.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,23 @@ public class MediaType extends MimeType implements Serializable {
9696

9797
/**
9898
* Public constant media type for {@code application/json;charset=UTF-8}.
99+
*
100+
* <p>This {@link MediaType#APPLICATION_JSON} variant should be used to set JSON
101+
* content type because while
102+
* <a href="https://tools.ietf.org/html/rfc7159#section-11">RFC7159</a>
103+
* clearly states that "no charset parameter is defined for this registration", some
104+
* browsers require it for interpreting correctly UTF-8 special characters.
99105
*/
100106
public static final MediaType APPLICATION_JSON_UTF8;
101107

102108
/**
103109
* A String equivalent of {@link MediaType#APPLICATION_JSON_UTF8}.
110+
*
111+
* <p>This {@link MediaType#APPLICATION_JSON_VALUE} variant should be used to set JSON
112+
* content type because while
113+
* <a href="https://tools.ietf.org/html/rfc7159#section-11">RFC7159</a>
114+
* clearly states that "no charset parameter is defined for this registration", some
115+
* browsers require it for interpreting correctly UTF-8 special characters.
104116
*/
105117
public static final String APPLICATION_JSON_UTF8_VALUE = "application/json;charset=UTF-8";
106118

spring-web/src/main/java/org/springframework/web/bind/annotation/RequestMapping.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,10 @@
193193
* <pre class="code">
194194
* produces = "text/plain"
195195
* produces = {"text/plain", "application/*"}
196-
* produces = "application/json; charset=UTF-8"
196+
* produces = MediaType.APPLICATION_JSON_UTF8_VALUE
197197
* </pre>
198198
* <p>It affects the actual content type written, for example to produce a JSON response
199-
* with UTF-8 encoding, {@code "application/json; charset=UTF-8"} should be used.
199+
* with UTF-8 encoding, {@link org.springframework.http.MediaType#APPLICATION_JSON_UTF8_VALUE} should be used.
200200
* <p>Expressions can be negated by using the "!" operator, as in "!text/plain", which matches
201201
* all requests with a {@code Accept} other than "text/plain".
202202
* <p><b>Supported at the type level as well as at the method level!</b>

src/docs/asciidoc/web/webflux.adoc

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,7 +1192,7 @@ overrides rather than extend the class level declaration.
11921192
[TIP]
11931193
====
11941194
`MediaType` provides constants for commonly used media types -- e.g.
1195-
`APPLICATION_JSON_VALUE`, `APPLICATION_JSON_UTF8_VALUE`.
1195+
`APPLICATION_JSON_VALUE`, `APPLICATION_XML_VALUE`.
11961196
====
11971197

11981198

@@ -1216,14 +1216,22 @@ content types that a controller method produces:
12161216
The media type can specify a character set. Negated expressions are supported -- e.g.
12171217
`!text/plain` means any content type other than "text/plain".
12181218

1219+
[NOTE]
1220+
====
1221+
For JSON content type, the UTF-8 charset should be specified even if
1222+
https://tools.ietf.org/html/rfc7159#section-11[RFC7159]
1223+
clearly states that "no charset parameter is defined for this registration" because some
1224+
browsers require it for interpreting correctly UTF-8 special characters.
1225+
====
1226+
12191227
You can declare a shared produces attribute at the class level. Unlike most other request
12201228
mapping attributes however when used at the class level, a method-level produces attribute
12211229
overrides rather than extend the class level declaration.
12221230

12231231
[TIP]
12241232
====
12251233
`MediaType` provides constants for commonly used media types -- e.g.
1226-
`APPLICATION_JSON_VALUE`, `APPLICATION_JSON_UTF8_VALUE`.
1234+
`APPLICATION_JSON_UTF8_VALUE`, `APPLICATION_XML_VALUE`.
12271235
====
12281236

12291237

src/docs/asciidoc/web/webmvc.adoc

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,7 +1425,7 @@ will overrides rather than extend the class level declaration.
14251425
[TIP]
14261426
====
14271427
`MediaType` provides constants for commonly used media types -- e.g.
1428-
`APPLICATION_JSON_VALUE`, `APPLICATION_JSON_UTF8_VALUE`.
1428+
`APPLICATION_JSON_VALUE`, `APPLICATION_XML_VALUE`.
14291429
====
14301430

14311431

@@ -1449,14 +1449,22 @@ content types that a controller method produces:
14491449
The media type can specify a character set. Negated expressions are supported -- e.g.
14501450
`!text/plain` means any content type other than "text/plain".
14511451

1452+
[NOTE]
1453+
====
1454+
For JSON content type, the UTF-8 charset should be specified even if
1455+
https://tools.ietf.org/html/rfc7159#section-11[RFC7159]
1456+
clearly states that "no charset parameter is defined for this registration" because some
1457+
browsers require it for interpreting correctly UTF-8 special characters.
1458+
====
1459+
14521460
You can declare a shared produces attribute at the class level. Unlike most other request
14531461
mapping attributes however when used at the class level, a method-level produces attribute
14541462
will overrides rather than extend the class level declaration.
14551463

14561464
[TIP]
14571465
====
14581466
`MediaType` provides constants for commonly used media types -- e.g.
1459-
`APPLICATION_JSON_VALUE`, `APPLICATION_JSON_UTF8_VALUE`.
1467+
`APPLICATION_JSON_UTF8_VALUE`, `APPLICATION_XML_VALUE`.
14601468
====
14611469

14621470

0 commit comments

Comments
 (0)