MockMvc no longer handles UTF-8 characters with Spring Boot 2.2.0.RELEASE

Using .getContentAsString(StandardCharsets.UTF_8) instead of .getContentAsString() resolves the problem.


Yes. This is problem from 2.2.0 spring-boot. They set deprecation for default charset encoding.

.getContentAsString(StandardCharsets.UTF_8) - good but in any response would be populated ISO 8859-1 by default.

In my project I updated current created converter:

@Configuration
public class SpringConfig implements WebMvcConfigurer {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.stream()
            .filter(converter -> converter instanceof MappingJackson2HttpMessageConverter)
            .findFirst()
            .ifPresent(converter -> ((MappingJackson2HttpMessageConverter) converter).setDefaultCharset(UTF_8));
    }
...

The default encoding character is no longer UTF-8 since the 5.2.0 version of spring.

To continue using UTF-8, you must set it in the ServletResponse of the MockMvc result. To set the default character encoding to UTF-8, do something like this in your setup method:

@Before
public void setUp() {
   mockMvc = webAppContextSetup(wac).addFilter(((request, response, chain) -> {
                response.setCharacterEncoding("UTF-8");
                chain.doFilter(request, response);
            })).build();
}

Then you can use the mockMvc instance to perform your request.

Hope this help.