The front-end/postman requests to the background, and the background fetches data with garbled Chinese characters, resulting in garbled records stored in the database——solution

operate

We get the data from the front end and store it in the database. Here, postman is used to simulate the value transfer from the front end to the background, as follows:
incoming parameters

Questions raised:

The record is added successfully, but the newly added record has garbled characters in Chinese, as follows: Obviously
The records stored in the database are garbled in Chinese
, this does not meet the effect we want, so how to solve this problem?

Solution steps


1. Identify the problem

Step1: Check the database character encoding

  • Select the database and ->右键 ->数据库属性confirm that the character encoding of the database is utf-8.

View database properties database character encoding

  • Check the character encoding of the database table, 选中数据库表->右键->对象信息, confirm that the character encoding of the database table is utf-8
    View database table object information Database table object information

Step2: After we have checked the database and there is no problem, the next step is to debug what is wrong with the background program. Start the server and enable debug mode.

A problem has arisen~ You can see that when the request gets the parameters, the characters are already garbled, so the value stored in the database is also garbled~
question

2. Solve the problem

Let's check the server.xml configuration file of the tomcat server.
server.xml

Found that there is no character encoding set

<Connector connectionTimeout="20000" port="8088" protocol="HTTP/1.1" redirectPort="8443"/>

So, let's try to add attributes Connectorin this tagURIEncoding

<Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8088" protocol="HTTP/1.1" redirectPort="8443"/>

At this time, we restart the server, use the debug mode to debug our program again, and find that we have successfully obtained Chinese~
debug successfully

At this point, we check the records stored in the database, and the stored values ​​are normal~perfect solution~
The database inventory value is successful~

Guess you like

Origin blog.csdn.net/pjymyself/article/details/82458645