Spring Boot
CharacterEncodingFilter
Java
Spring Framework
Configuration Guide

How to configure CharacterEncodingFilter in SpringBoot?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Character encoding problems usually show up as garbled text, broken non-ASCII form input, or inconsistent request and response behavior. In Spring Boot, the usual solution is CharacterEncodingFilter, which ensures that requests and responses use a consistent encoding such as UTF-8.

The good news is that modern Spring Boot applications often do not need a custom filter bean at all. Spring Boot can auto-configure the encoding filter for you through application properties, and that is usually the cleanest approach.

The Simplest Configuration: Application Properties

For most applications, configure encoding through properties.

In application.properties:

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

Or in application.yml:

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

This tells Spring Boot to register and configure the encoding filter so incoming requests and outgoing responses use UTF-8 consistently.

What These Properties Mean

The main properties are:

  • 'spring.servlet.encoding.enabled: enables encoding support'
  • 'spring.servlet.encoding.charset: sets the default charset'
  • 'spring.servlet.encoding.force: forces the configured encoding onto requests and responses'

For many web applications, UTF-8 is the right default because it handles international text safely and matches modern browser expectations.

Manual Bean Configuration When You Need More Control

If you need to configure the filter explicitly, you can define a CharacterEncodingFilter bean yourself.

java
1import org.springframework.context.annotation.Bean;
2import org.springframework.context.annotation.Configuration;
3import org.springframework.web.filter.CharacterEncodingFilter;
4
5@Configuration
6public class WebEncodingConfig {
7
8    @Bean
9    public CharacterEncodingFilter characterEncodingFilter() {
10        CharacterEncodingFilter filter = new CharacterEncodingFilter();
11        filter.setEncoding("UTF-8");
12        filter.setForceEncoding(true);
13        return filter;
14    }
15}

This is useful when you want filter-level customization in code rather than in external configuration.

When Manual Configuration Is Actually Necessary

In many Boot applications, manual bean creation is unnecessary because the property-based configuration already produces the right filter behavior.

A custom bean is more appropriate when:

  • you need special registration behavior
  • you are integrating with custom servlet filter ordering
  • you want the configuration expressed directly in Java code
  • you are working in a codebase where application properties are centrally controlled and not easy to change

If none of those apply, the properties are simpler.

Why Encoding Problems Happen

HTTP requests do not always arrive with the encoding you expect, especially in older integrations or mixed environments. If the server interprets request bytes with the wrong charset, characters such as accented letters or non-Latin scripts become corrupted before your application logic ever sees them.

That is why the encoding filter matters early in the request lifecycle. It standardizes how the request is interpreted before controllers and request-body parsing proceed.

A Small Example Controller

Once encoding is configured, a controller that receives UTF-8 text behaves normally:

java
1import org.springframework.web.bind.annotation.PostMapping;
2import org.springframework.web.bind.annotation.RequestBody;
3import org.springframework.web.bind.annotation.RestController;
4
5@RestController
6public class MessageController {
7
8    @PostMapping("/messages")
9    public String echo(@RequestBody String body) {
10        return body;
11    }
12}

If a client posts text containing characters from multiple languages, the application is much less likely to corrupt them when the encoding filter is configured correctly.

Do Not Forget the Client Side

Server-side encoding is only part of the story. If the client sends the wrong Content-Type header or the HTML page itself declares the wrong charset, problems can still appear.

For HTML pages, make sure the page declares UTF-8 as well:

html
<meta charset="UTF-8">

Consistency across browser, server, and database is what prevents encoding bugs from reappearing in strange places.

Common Pitfalls

One common mistake is manually registering CharacterEncodingFilter while also relying on Spring Boot auto-configuration without understanding which one wins. That can make debugging confusing.

Another issue is fixing Spring Boot encoding while leaving the client or database on a different charset. Encoding problems are end-to-end problems, not just filter problems.

It is also easy to copy outdated property names from old Spring Boot examples. Modern Boot uses the spring.servlet.encoding.* namespace.

Finally, do not assume the filter is needed only for responses. Request decoding is often where the real corruption happens first.

Summary

  • In Spring Boot, the easiest way to configure CharacterEncodingFilter is through spring.servlet.encoding.* properties.
  • UTF-8 is the usual default choice for modern applications.
  • A manual CharacterEncodingFilter bean is only needed when you want more explicit control.
  • Encoding must be consistent across request handling, response generation, HTML pages, and storage layers.
  • Many real encoding bugs come from mixed configuration rather than from Spring Boot alone.

Related reading
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
Track 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.

Browse interview questions

All Rights Reserved.