32

I encountered some encoding problems in learning Spring Boot; I want to add a CharacterEncodingFilter like Spring 3.x. just like this:

<filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
Mohit Kanwar
  • 2,659
  • 6
  • 33
  • 52
navee
  • 531
  • 1
  • 5
  • 12

4 Answers4

71

Since Spring Boot 1.4.2 registering your own CharacterEncodingFilter will work ONLY IF you disable Spring's own instance of this bean by setting spring.http.encoding.enabled=false in the application.properties.

However, one can resolve this matter without any Filter instantiation by adding these setting to the application.properties:

# Charset of HTTP requests and responses. Added to the "Content-Type" header if not set explicitly.
spring.http.encoding.charset=UTF-8
# Enable http encoding support.
spring.http.encoding.enabled=true
# Force the encoding to the configured charset on HTTP requests and responses.
spring.http.encoding.force=true

Source: Appendix A. Common application properties

informatik01
  • 15,174
  • 9
  • 67
  • 100
Fritz Duchardt
  • 8,238
  • 2
  • 33
  • 52
  • 4
    You are right, using properties is the better choice. – mika Jan 27 '16 at 21:05
  • 1
    Since spring boot 2.3: The spring.http. properties have been moved to server.servlet.encoding., spring.mvc. and spring.codec., see #18827. – Wooff Jun 29 '20 at 09:54
16

Example code for your Application.java class, as proposed in the comments above:

@Bean
public FilterRegistrationBean filterRegistrationBean() {
    FilterRegistrationBean registrationBean = new FilterRegistrationBean();
    CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
    characterEncodingFilter.setForceEncoding(true);
    characterEncodingFilter.setEncoding("UTF-8");
    registrationBean.setFilter(characterEncodingFilter);
    return registrationBean;
}
Fritz Duchardt
  • 8,238
  • 2
  • 33
  • 52
mika
  • 2,321
  • 19
  • 26
6

I also prefer application.properties configuration. But spring.http.encoding is depracted in the new spring boot versions (>2.3). So new application.setting should look like this:

server.servlet.encoding.charset=UTF-8
server.servlet.encoding.enabled=true
server.servlet.encoding.force=true
Maksym Pecheniuk
  • 1,695
  • 3
  • 23
  • 38
-8

I think there is no need to explicity write the following properties in application.properties file:

spring.http.encoding.charset=UTF-8

spring.http.encoding.enabled=true

spring.http.encoding.force=true

Instead if you go to pom.xml in your application and if you have the following, then spring will do the needful.

Property