Spring Boot
UTF-8
Character Encoding
Java
Application Configuration

How to set UTF-8 character encoding in Spring Boot

Codemia Knowledge Hub ·

Set the server.servlet.encoding properties to UTF-8 and force them, then check that every other layer (source files, templates, the JDBC connection and the database) also uses UTF-8. In a Spring Boot web app, garbled non-ASCII text in request bodies, form posts or responses usually means one layer read the bytes with the wrong encoding.

Servlet encoding properties

For servlet-based Spring Boot applications, these properties are the standard place to configure it.

In application.properties:

properties
server.servlet.encoding.charset=UTF-8
server.servlet.encoding.enabled=true
server.servlet.encoding.force=true

Or in application.yml:

yaml
1server:
2  servlet:
3    encoding:
4      charset: UTF-8
5      enabled: true
6      force: true

This makes UTF-8 the default request and response encoding and forces it wherever the servlet stack supports that.

These settings cover the HTTP layer:

  • Reading request parameters and form submissions.
  • Writing response content.
  • Keeping servlet-level text processing consistent.

If characters are still corrupted afterwards, the problem is somewhere else, such as the database, a template file or a downstream service.

No encoding code needed in controllers

When the app is configured correctly, a controller needs no manual encoding handling:

java
1import org.springframework.web.bind.annotation.GetMapping;
2import org.springframework.web.bind.annotation.RestController;
3
4@RestController
5public class GreetingController {
6
7    @GetMapping("/greeting")
8    public String greeting() {
9        return "Olá, こんにちは, مرحبا";
10    }
11}

The response comes out correctly without extra work in the controller.

Source files and templates saved in the wrong encoding

Spring Boot settings cannot fix text that is already stored wrongly. If a .java, .html, .sql or template file is saved in another encoding, the output can still be corrupted. Check 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.

A single file saved incorrectly is a more common cause than the Spring Boot properties.

Database and JDBC encoding

If the app stores multilingual text, the database and connection must support UTF-8 too. A UTF-8 web layer cannot make up for a database with incompatible character settings. A full diagnosis can involve:

  • Spring Boot servlet encoding.
  • Template or resource file encoding.
  • JDBC connection behavior.
  • Database column and table character settings.

If persisted data is already wrong, the HTTP layer is not the place to look.

When to use CharacterEncodingFilter

In most modern Spring Boot setups the properties are enough. In an unusual legacy configuration you can still register a custom CharacterEncodingFilter, but only add it when the built-in settings do not solve the problem.

Mistakes that keep text garbled

  • Setting UTF-8 for HTTP responses while source or template files use another encoding.
  • Expecting servlet encoding settings to fix database-level character problems.
  • Copying older property names from outdated examples instead of the current server.servlet.encoding properties.
  • Adding custom filters before checking whether the Boot configuration already covers the case.
  • Debugging controller code when the corruption happened earlier in the pipeline.

Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms