Spring RestController生成charset = UTF-8

时间:2016-09-30 09:46:29

标签: java spring-mvc spring-boot

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

控制器:

Group By f.username

响应标题

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

现在是否有其他配置的地方覆盖了RequestMapping设置?

1 个答案:

答案 0 :(得分:7)

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

可以通过实现下面的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;
}