Spring Boot
MockMvc
UTF-8
Spring Framework
Encoding Issues

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

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Understanding MockMvc and UTF-8 Handling in Spring Boot 2.2.0.RELEASE

Spring Boot and Spring Framework have long been trusted tools for Java developers, offering a comprehensive suite of utilities designed to simplify the development process. However, with the release of Spring Boot 2.2.0.RELEASE, developers began to see unexpected behavior concerning the handling of UTF-8 characters when using MockMvc for testing. In this article, we will explore the technical underpinnings of this issue, offer examples, and review some possible workarounds.

What is MockMvc?

`MockMvc` is a part of the Spring Test framework that allows you to test your Spring MVC application through a fluent API. It provides a way to execute HTTP requests in a test environment and simulate interactions with the server-side components, without the browser. This testing utility is invaluable during development for ensuring that your web application behaves as expected under various conditions.

UTF-8 Encoding in Web Applications

UTF-8 is a widely used character encoding that is capable of encoding all possible characters, or code points, in Unicode. It's essential for supporting internationalization and enabling software to process languages like Chinese, Arabic, and Russian. Java applications generally handle UTF-8 seamlessly, but there occur situations where encoding issues emerge, especially in web contexts when dealing with form submissions, query parameters, and JSON payloads.

Issue in Spring Boot 2.2.0.RELEASE

With the Spring Boot 2.2.0.RELEASE, developers have reported that `MockMvc` no longer correctly handles UTF-8 encoded characters by default. This change primarily affects the way JSON payloads and request parameters are processed in unit tests. The root of this issue is a change in how character encoding is specified and handled within the framework, leading to defaults that may not align with previous configurations.

Example of the Problem

Consider the following test setup, where `MockMvc` is used to submit a JSON payload containing UTF-8 characters:


Course illustration
Course illustration

All Rights Reserved.