当前位置: 首页 > 知识库问答 >
问题:

Spring RestController产生charset=UTF-8

洪弘毅
2023-03-14

自从更新到最新版本的Spring-Boot (1.4.1)以来,我注意到在我的RestControllers中,即使我明确地将产生的媒体类型设置为“application/json ”,它现在也产生“application/JSON;字符集=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设置?

共有1个答案

蔚丰
2023-03-14

根据OrangeDog上面的评论,MappingJackson2HttpMessageConverter处理字符集。如果消息中没有指定任何字符集,这最近已更新以添加默认字符集(即通过请求映射生成配置)

这可以通过实现以下bean并将charSet设置为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;
}
 类似资料: