Java – Spring RestController produces charset=UTF-8

javaspring-bootspring-mvc

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"

Controller:

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

Response Header

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

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

Best Answer

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)

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;
}