diff --git a/spring-web/src/main/java/org/springframework/http/MediaType.java b/spring-web/src/main/java/org/springframework/http/MediaType.java index 53a2dde9ec..8b8e04853f 100644 --- a/spring-web/src/main/java/org/springframework/http/MediaType.java +++ b/spring-web/src/main/java/org/springframework/http/MediaType.java @@ -40,6 +40,7 @@ import org.springframework.util.comparator.CompoundComparator; * @author Arjen Poutsma * @author Juergen Hoeller * @author Rossen Stoyanchev + * @author Sebastien Deleuze * @since 3.0 * @see HTTP 1.1: Semantics * and Content, section 3.1.1.1 @@ -80,14 +81,26 @@ public class MediaType extends MimeType implements Serializable { /** * Public constant media type for {@code application/json}. - * */ + * @see #APPLICATION_JSON_UTF8 + */ public final static MediaType APPLICATION_JSON; /** * A String equivalent of {@link MediaType#APPLICATION_JSON}. + * @see #APPLICATION_JSON_UTF8_VALUE */ public final static String APPLICATION_JSON_VALUE = "application/json"; + /** + * Public constant media type for {@code application/json;charset=UTF-8}. + */ + public final static MediaType APPLICATION_JSON_UTF8; + + /** + * A String equivalent of {@link MediaType#APPLICATION_JSON_UTF8}. + */ + public final static String APPLICATION_JSON_UTF8_VALUE = APPLICATION_JSON_VALUE + ";charset=UTF-8"; + /** * Public constant media type for {@code application/octet-stream}. * */ @@ -197,6 +210,7 @@ public class MediaType extends MimeType implements Serializable { APPLICATION_ATOM_XML = valueOf(APPLICATION_ATOM_XML_VALUE); APPLICATION_FORM_URLENCODED = valueOf(APPLICATION_FORM_URLENCODED_VALUE); APPLICATION_JSON = valueOf(APPLICATION_JSON_VALUE); + APPLICATION_JSON_UTF8 = valueOf(APPLICATION_JSON_UTF8_VALUE); APPLICATION_OCTET_STREAM = valueOf(APPLICATION_OCTET_STREAM_VALUE); APPLICATION_XHTML_XML = valueOf(APPLICATION_XHTML_XML_VALUE); APPLICATION_XML = valueOf(APPLICATION_XML_VALUE); diff --git a/spring-web/src/main/java/org/springframework/http/converter/json/GsonHttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/json/GsonHttpMessageConverter.java index 1e795fb30f..1cfe37ead3 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/json/GsonHttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/json/GsonHttpMessageConverter.java @@ -45,7 +45,8 @@ import org.springframework.util.Assert; * {@link Gson} class. * *

This converter can be used to bind to typed beans or untyped {@code HashMap}s. - * By default, it supports {@code application/json} and {@code application/*+json}. + * By default, it supports {@code application/json} and {@code application/*+json} with + * {@code UTF-8} character set. * *

Tested against Gson 2.3; compatible with Gson 2.0 and higher. * @@ -69,8 +70,7 @@ public class GsonHttpMessageConverter extends AbstractGenericHttpMessageConverte * Construct a new {@code GsonHttpMessageConverter}. */ public GsonHttpMessageConverter() { - super(new MediaType("application", "json", DEFAULT_CHARSET), - new MediaType("application", "*+json", DEFAULT_CHARSET)); + super(MediaType.APPLICATION_JSON_UTF8, new MediaType("application", "*+json", DEFAULT_CHARSET)); } diff --git a/spring-web/src/main/java/org/springframework/http/converter/json/MappingJackson2HttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/json/MappingJackson2HttpMessageConverter.java index f8a4df65d9..80f9b430a3 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/json/MappingJackson2HttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/json/MappingJackson2HttpMessageConverter.java @@ -63,7 +63,7 @@ public class MappingJackson2HttpMessageConverter extends AbstractJackson2HttpMes * @see Jackson2ObjectMapperBuilder#json() */ public MappingJackson2HttpMessageConverter(ObjectMapper objectMapper) { - super(objectMapper, new MediaType("application", "json", DEFAULT_CHARSET), + super(objectMapper, MediaType.APPLICATION_JSON_UTF8, new MediaType("application", "*+json", DEFAULT_CHARSET)); } diff --git a/src/asciidoc/web-mvc.adoc b/src/asciidoc/web-mvc.adoc index f68f8d7562..669fe744fa 100644 --- a/src/asciidoc/web-mvc.adoc +++ b/src/asciidoc/web-mvc.adoc @@ -1028,7 +1028,7 @@ condition. For example: [subs="verbatim,quotes"] ---- @Controller - @RequestMapping(path = "/pets/{petId}", method = RequestMethod.GET, **produces="application/json; charset=UTF-8"**) + @RequestMapping(path = "/pets/{petId}", method = RequestMethod.GET, **produces = MediaType.APPLICATION_JSON_UTF8_VALUE**) @ResponseBody public Pet getPet(@PathVariable String petId, Model model) { // implementation omitted @@ -3855,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; charset=UTF-8") + @RequestMapping(path = "/error", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) @ResponseBody public Map handle(HttpServletRequest request) {