Skip to content

Commit 62cd6ad

Browse files
committed
Add @RequestMapping produces documentation about charset
Issue: SPR-13600
1 parent 4aedd92 commit 62cd6ad

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,8 +414,11 @@
414414
* <pre class="code">
415415
* produces = "text/plain"
416416
* produces = {"text/plain", "application/*"}
417+
* produces = "application/json; charset=UTF-8"
417418
* </pre>
418-
* Expressions can be negated by using the "!" operator, as in "!text/plain", which matches
419+
* <p>It affects the actual content type written, for example to produce a JSON response
420+
* with UTF-8 encoding, {@code "application/json; charset=UTF-8"} should be used.
421+
* <p>Expressions can be negated by using the "!" operator, as in "!text/plain", which matches
419422
* all requests with a {@code Accept} other than "text/plain".
420423
* <p><b>Supported at the type level as well as at the method level!</b>
421424
* When used at the type level, all method-level mappings override

src/asciidoc/web-mvc.adoc

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,13 +1028,21 @@ condition. For example:
10281028
[subs="verbatim,quotes"]
10291029
----
10301030
@Controller
1031-
@RequestMapping(path = "/pets/{petId}", method = RequestMethod.GET, **produces="application/json"**)
1031+
@RequestMapping(path = "/pets/{petId}", method = RequestMethod.GET, **produces="application/json; charset=UTF-8"**)
10321032
@ResponseBody
10331033
public Pet getPet(@PathVariable String petId, Model model) {
10341034
// implementation omitted
10351035
}
10361036
----
10371037

1038+
[NOTE]
1039+
====
1040+
Be aware that the media type specified in the __produces__ condition can also optionally
1041+
specify a character set. For example, in the code snippet above we specify the same media
1042+
type than the default one configured in `MappingJackson2HttpMessageConverter`, including
1043+
the `UTF-8` charset.
1044+
====
1045+
10381046
Just like with __consumes__, producible media type expressions can be negated as in
10391047
__!text/plain__ to match to all requests other than those with an __Accept__ header
10401048
value of __text/plain__.
@@ -3847,7 +3855,7 @@ When writing error information, the status code and the error message set on the
38473855
@Controller
38483856
public class ErrorController {
38493857
3850-
@RequestMapping(path="/error", produces="application/json")
3858+
@RequestMapping(path="/error", produces="application/json; charset=UTF-8")
38513859
@ResponseBody
38523860
public Map<String, Object> handle(HttpServletRequest request) {
38533861

0 commit comments

Comments
 (0)