Browse Source

Add MediaType.APPLICATION_JSON_UTF8

Add new MediaType.APPLICATION_JSON_UTF8 and
MediaType.APPLICATION_JSON_UTF8_VALUE constants for
"application/json;charset=UTF-8" content type in order to make it
easier to override @RequestMapping "produces" attribute without
losing the default JSON charset (UTF-8).

Issue: SPR-13600
pull/910/head
Sebastien Deleuze 11 years ago
parent
commit
09cb286c84
  1. 16
      spring-web/src/main/java/org/springframework/http/MediaType.java
  2. 6
      spring-web/src/main/java/org/springframework/http/converter/json/GsonHttpMessageConverter.java
  3. 2
      spring-web/src/main/java/org/springframework/http/converter/json/MappingJackson2HttpMessageConverter.java
  4. 4
      src/asciidoc/web-mvc.adoc

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

@ -40,6 +40,7 @@ import org.springframework.util.comparator.CompoundComparator;
* @author Arjen Poutsma * @author Arjen Poutsma
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @author Sebastien Deleuze
* @since 3.0 * @since 3.0
* @see <a href="http://tools.ietf.org/html/rfc7231#section-3.1.1.1">HTTP 1.1: Semantics * @see <a href="http://tools.ietf.org/html/rfc7231#section-3.1.1.1">HTTP 1.1: Semantics
* and Content, section 3.1.1.1</a> * and Content, section 3.1.1.1</a>
@ -80,14 +81,26 @@ public class MediaType extends MimeType implements Serializable {
/** /**
* Public constant media type for {@code application/json}. * Public constant media type for {@code application/json}.
* */ * @see #APPLICATION_JSON_UTF8
*/
public final static MediaType APPLICATION_JSON; public final static MediaType APPLICATION_JSON;
/** /**
* A String equivalent of {@link 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 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}. * 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_ATOM_XML = valueOf(APPLICATION_ATOM_XML_VALUE);
APPLICATION_FORM_URLENCODED = valueOf(APPLICATION_FORM_URLENCODED_VALUE); APPLICATION_FORM_URLENCODED = valueOf(APPLICATION_FORM_URLENCODED_VALUE);
APPLICATION_JSON = valueOf(APPLICATION_JSON_VALUE); APPLICATION_JSON = valueOf(APPLICATION_JSON_VALUE);
APPLICATION_JSON_UTF8 = valueOf(APPLICATION_JSON_UTF8_VALUE);
APPLICATION_OCTET_STREAM = valueOf(APPLICATION_OCTET_STREAM_VALUE); APPLICATION_OCTET_STREAM = valueOf(APPLICATION_OCTET_STREAM_VALUE);
APPLICATION_XHTML_XML = valueOf(APPLICATION_XHTML_XML_VALUE); APPLICATION_XHTML_XML = valueOf(APPLICATION_XHTML_XML_VALUE);
APPLICATION_XML = valueOf(APPLICATION_XML_VALUE); APPLICATION_XML = valueOf(APPLICATION_XML_VALUE);

6
spring-web/src/main/java/org/springframework/http/converter/json/GsonHttpMessageConverter.java

@ -45,7 +45,8 @@ import org.springframework.util.Assert;
* {@link Gson} class. * {@link Gson} class.
* *
* <p>This converter can be used to bind to typed beans or untyped {@code HashMap}s. * <p>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.
* *
* <p>Tested against Gson 2.3; compatible with Gson 2.0 and higher. * <p>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}. * Construct a new {@code GsonHttpMessageConverter}.
*/ */
public GsonHttpMessageConverter() { public GsonHttpMessageConverter() {
super(new MediaType("application", "json", DEFAULT_CHARSET), super(MediaType.APPLICATION_JSON_UTF8, new MediaType("application", "*+json", DEFAULT_CHARSET));
new MediaType("application", "*+json", DEFAULT_CHARSET));
} }

2
spring-web/src/main/java/org/springframework/http/converter/json/MappingJackson2HttpMessageConverter.java

@ -63,7 +63,7 @@ public class MappingJackson2HttpMessageConverter extends AbstractJackson2HttpMes
* @see Jackson2ObjectMapperBuilder#json() * @see Jackson2ObjectMapperBuilder#json()
*/ */
public MappingJackson2HttpMessageConverter(ObjectMapper objectMapper) { public MappingJackson2HttpMessageConverter(ObjectMapper objectMapper) {
super(objectMapper, new MediaType("application", "json", DEFAULT_CHARSET), super(objectMapper, MediaType.APPLICATION_JSON_UTF8,
new MediaType("application", "*+json", DEFAULT_CHARSET)); new MediaType("application", "*+json", DEFAULT_CHARSET));
} }

4
src/asciidoc/web-mvc.adoc

@ -1028,7 +1028,7 @@ condition. For example:
[subs="verbatim,quotes"] [subs="verbatim,quotes"]
---- ----
@Controller @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 @ResponseBody
public Pet getPet(@PathVariable String petId, Model model) { public Pet getPet(@PathVariable String petId, Model model) {
// implementation omitted // implementation omitted
@ -3855,7 +3855,7 @@ When writing error information, the status code and the error message set on the
@Controller @Controller
public class ErrorController { public class ErrorController {
@RequestMapping(path="/error", produces="application/json; charset=UTF-8") @RequestMapping(path = "/error", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@ResponseBody @ResponseBody
public Map<String, Object> handle(HttpServletRequest request) { public Map<String, Object> handle(HttpServletRequest request) {

Loading…
Cancel
Save