Spring RestController生成charset = UTF-8 [英] Spring RestController produces charset=UTF-8

查看:1578
本文介绍了Spring RestController生成charset = UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从更新到最新版本的Spring-Boot(1.4.1)后,我注意到在我的RestControllers中,即使我明确地将生成的媒体类型设置为application / json,它现在正在生成application / json; charset = UTF-8

Since updating to the latest version of Spring-Boot (1.4.1) I've noticed that in my RestControllers even though I'm explicitly setting the media type produced to "application/json" it is now producing "application/json;charset=UTF-8"

控制器:

@RestController
@RequestMapping(value = "/api/1/accounts", consumes = "application/json", produces = "application/json")
public class AccountController {
.....

响应标题

Content-Type →application/json;charset=UTF-8

是现在在其他地方配置了哪个覆盖了RequestMapping设置?

Is there now somewhere else where this is configured which is overriding the RequestMapping setting?

推荐答案

根据OrangeDog上面的注释,MappingJackson2HttpMessageConverter处理字符集。如果没有在消息中指定(即通过RequestMapping生成配置),则最近已更新以添加默认charSet

As per OrangeDog's comment above the MappingJackson2HttpMessageConverter handles the charset. This has been updated recently to add the default charSet if none is specified in message (i.e. via the RequestMapping produces config)

这可以通过实现以下bean和设置来覆盖charSet为null:

This can be overridden by implementing the below bean and setting the charSet to null:

@Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
    MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    jsonConverter.setObjectMapper(objectMapper);
    jsonConverter.setDefaultCharset(null);
    return jsonConverter;
}

这篇关于Spring RestController生成charset = UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆