Good day.

There is a simple CRUD application on Spring Boot.

In the IDE, everything works as it should, no problem.

Generated WAR, zadepil in TomCat - everything works there too, except for the encoding when sending data from the form - krakozyabry are written, i.e. instead of "test" in the database goes "Р Ð ÐµÑÑ". Google, but alas ...

In application.properties:

spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp spring.data.jpa.repositories.enabled=true spring.jpa.generate.ddl=true spring.jpa.hibernate.ddl-auto=create spring.jpa.show-sql=true spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8 spring.datasource.username=root spring.datasource.password=root spring.datasource.sqlScriptEncoding=UTF-8 spring.messages.encoding=UTF-8 server.tomcat.uri-encoding=UTF-8 spring.http.encoding.charset=UTF-8 

In pom.xml there is:

 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 

In web.xml there is:

  <filter> <filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app> 

In Application.java there are:

 @Bean public FilterRegistrationBean filterRegistrationBean() { FilterRegistrationBean registrationBean = new FilterRegistrationBean(); CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter(); characterEncodingFilter.setForceEncoding(true); characterEncodingFilter.setEncoding("UTF-8"); registrationBean.setFilter(characterEncodingFilter); return registrationBean; } 

In .jsp are present:

 <%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %> 

and

 <meta charset="utf-8"> 

What else did I miss?

Update: A shortened working project http://web-site.ru/Library.rar

  • To check, try to display the data in the console, before they are written to the database, if everything is ok, then you need to watch the database. schema in the database should be created with the utf8_general_ci coding, and not by default in latin1 , check this encoding. - MrFylypenko
  • I looked at tomcat8-stdout.log, there is "??? µ ???? 1" instead of "test 1". In the database, of course, utf8_general_ci - apollox
  • The online converter decrypted the ТеÑÑ as ыцее , and the original encoding is cp866 . By default, the windows cp866 console and there is no utf8 support, there are suspicions that through it and see. Add information where you start the project (locally, remotely, which axis), how and through what you look at the logs. And ideally, lay out a minimal reproducible example on a githab, or in a question. - MrFylypenko
  • I launch it on Win 10 on localhost, in Idea everything is ok, after deploying to TomCat - krakozyabry ... MySQL connection parameters in application.properties, the table is created using data.sql - apollox
  • Try to remove the filter in web.xml, it seems that the filter is applied 2 times. - MrFylypenko

1 answer 1

The problem was the misuse of the CharacterEncodingFilter . This filter was used in 2 places at the same time:

  1. in the web.xml file
  2. registered with a separate bean in the java configuration

It is necessary to remove the filter in the web.xml file, and after that, requests in the normal encoding will come to the application from the browser.