How to set UTF-8 character encoding in Spring boot?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In a Spring Boot web application, UTF-8 problems usually appear when request bodies, form posts, or responses contain non-ASCII characters and one layer interprets them with the wrong encoding. The fix is not just "use UTF-8 somewhere." The fix is to make request handling, response generation, templates, and storage agree on UTF-8 consistently.
Start With the Web Encoding Settings
For servlet-based Spring Boot applications, the normal configuration point is the server servlet encoding properties.
In application.properties:
Or in application.yml:
This tells the application to use UTF-8 as the default request and response encoding and to force it consistently where the servlet stack supports that behavior.
What These Settings Actually Affect
The encoding settings mainly matter for the HTTP layer:
- Reading request parameters and form submissions.
- Writing response content.
- Keeping servlet-level text processing consistent.
If characters still look corrupted after setting them, the problem may be elsewhere, such as the database, a template file, or a downstream service.
Controller Example
A controller does not usually need manual encoding code if the app is configured correctly.
With UTF-8 configured properly, the response should be emitted correctly without extra work in the controller itself.
Do Not Forget the Source Files and Templates
Spring Boot settings cannot fix text that is already stored incorrectly. If your .java, .html, .sql, or template files are saved in the wrong encoding, the application may still produce corrupted output.
So verify that:
- Source files are saved as UTF-8.
- Template files are saved as UTF-8.
- Build tooling does not rewrite resources with a different encoding.
Encoding bugs often come from one incorrectly saved file rather than from the Spring Boot properties.
Database Encoding Is a Separate Layer
If your application reads or writes multilingual text to a database, the database and connection settings must also support UTF-8 correctly. A web layer configured for UTF-8 cannot fully compensate for a database configured with incompatible character settings.
That means a complete diagnosis may involve:
- Spring Boot servlet encoding.
- Template or resource file encoding.
- JDBC connection behavior.
- Database column and table character settings.
Do not stop at the HTTP layer if persisted data is already wrong.
When a Filter Is Still Relevant
In many modern Spring Boot setups, the property-based approach is enough. If you are working in an unusual legacy configuration, a custom CharacterEncodingFilter can still be used, but it should not be your first move if the standard Boot settings already solve the problem.
The best path is usually to start with built-in configuration and only add custom filter code if you have a real reason.
Common Pitfalls
- Setting UTF-8 for HTTP responses while leaving source or template files in another encoding.
- Assuming servlet encoding settings automatically fix database-level character issues.
- Copying older property names from outdated examples instead of using the current server servlet encoding configuration.
- Adding custom filters before checking whether Boot configuration already covers the use case.
- Debugging the controller code when the corruption actually happened earlier in the pipeline.
Summary
- Use the
server.servlet.encodingproperties to set UTF-8 in a Spring Boot web application. - Treat encoding as an end-to-end concern, not just a response-header setting.
- Make sure source files, templates, and database storage also use compatible encoding.
- Start with built-in Spring Boot configuration before adding custom filters.
- Most UTF-8 bugs come from one inconsistent layer in an otherwise correct pipeline.
Related reading
- How to setup multiple connection pools when multiple datasources are used in Spring Boot?
- How to setup multiple topics in a RabbitMQ Java config class using Spring Framework?
- How to setup pre-authentication header-based authentication in Spring Boot?
- How to show all parents and subclasses of a class in IntelliJ IDEA?
- How to shut down a Spring Boot command-line application
- How to shutdown a Spring Boot Application in a correct way?
- How to simplify a null-safe compareTo implementation?
- How to skip corrupt (non-serializable) messages in Spring Kafka Consumer?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.