提交 09cb286c 编写于 作者: S Sebastien Deleuze

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
上级 23c8f712
......@@ -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 <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>
......@@ -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);
......
......@@ -45,7 +45,8 @@ import org.springframework.util.Assert;
* {@link Gson} class.
*
* <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.
*
......@@ -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));
}
......
......@@ -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));
}
......
......@@ -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<String, Object> handle(HttpServletRequest request) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册