From 62cd6ad526dac77484af4737c102c3aae5bc82a9 Mon Sep 17 00:00:00 2001 From: Sebastien Deleuze Date: Mon, 2 Nov 2015 07:48:02 +0100 Subject: [PATCH] Add @RequestMapping produces documentation about charset Issue: SPR-13600 --- .../web/bind/annotation/RequestMapping.java | 5 ++++- src/asciidoc/web-mvc.adoc | 12 ++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/bind/annotation/RequestMapping.java b/spring-web/src/main/java/org/springframework/web/bind/annotation/RequestMapping.java index 4d5286d4e26..c884775c867 100644 --- a/spring-web/src/main/java/org/springframework/web/bind/annotation/RequestMapping.java +++ b/spring-web/src/main/java/org/springframework/web/bind/annotation/RequestMapping.java @@ -414,8 +414,11 @@ public @interface RequestMapping { *
 	 * produces = "text/plain"
 	 * produces = {"text/plain", "application/*"}
+	 * produces = "application/json; charset=UTF-8"
 	 * 
- * Expressions can be negated by using the "!" operator, as in "!text/plain", which matches + *

It affects the actual content type written, for example to produce a JSON response + * with UTF-8 encoding, {@code "application/json; charset=UTF-8"} should be used. + *

Expressions can be negated by using the "!" operator, as in "!text/plain", which matches * all requests with a {@code Accept} other than "text/plain". *

Supported at the type level as well as at the method level! * When used at the type level, all method-level mappings override diff --git a/src/asciidoc/web-mvc.adoc b/src/asciidoc/web-mvc.adoc index af0cca72dd6..f68f8d75629 100644 --- a/src/asciidoc/web-mvc.adoc +++ b/src/asciidoc/web-mvc.adoc @@ -1028,13 +1028,21 @@ condition. For example: [subs="verbatim,quotes"] ---- @Controller - @RequestMapping(path = "/pets/{petId}", method = RequestMethod.GET, **produces="application/json"**) + @RequestMapping(path = "/pets/{petId}", method = RequestMethod.GET, **produces="application/json; charset=UTF-8"**) @ResponseBody public Pet getPet(@PathVariable String petId, Model model) { // implementation omitted } ---- +[NOTE] +==== +Be aware that the media type specified in the __produces__ condition can also optionally +specify a character set. For example, in the code snippet above we specify the same media +type than the default one configured in `MappingJackson2HttpMessageConverter`, including +the `UTF-8` charset. +==== + Just like with __consumes__, producible media type expressions can be negated as in __!text/plain__ to match to all requests other than those with an __Accept__ header value of __text/plain__. @@ -3847,7 +3855,7 @@ When writing error information, the status code and the error message set on the @Controller public class ErrorController { - @RequestMapping(path="/error", produces="application/json") + @RequestMapping(path="/error", produces="application/json; charset=UTF-8") @ResponseBody public Map handle(HttpServletRequest request) {