简体   繁体   English

tomcat/spring 服务器 REST 输入编码错误

[英]Wrong encoding for tomcat/spring server REST input

I have a REST service made with Tomcat 8 and Spring v5.3.18.我有使用 Tomcat 8 和 Spring v5.3.18 制作的 REST 服务。 The service gets and stores a string in DB.该服务获取一个字符串并将其存储在数据库中。

I have an issue with this input:我对此输入有疑问:

{"name": "Pawe\u0142"}

It is a valid UTF8 char.它是一个有效的 UTF8 字符。 The string saved is Pawe?保存的字符串是Pawe? so there is something wrong...所以有什么不对...

I started to check where it can be the missing encoding, but I found that it depends on the tomcat environment.我开始检查可能是缺少编码的地方,但我发现它取决于 tomcat 环境。 Two servers with the same software , linked to a common database but with different operative system, first is Linux, second is Windows have different responses.具有相同软件的两台服务器,链接到一个公共数据库但具有不同的操作系统,第一个是 Linux,第二个是 Windows 有不同的响应。

The Windows server is working and store the valid UTF8 char, the linux server is not working and stores the char as "?". Windows 服务器正在工作并存储有效的 UTF8 字符,linux 服务器不工作并将字符存储为“?”。

So I deduced it is not related to the software or missing encoding in sw configuration, but probably it is related to tomcat or java.所以我推断它与软件或软件配置中缺少编码无关,但可能与 tomcat 或 java 有关。

Do you have any idea about what can be wrong?你知道什么是错误的吗?

Probable causes could be LC_CTYPE linux locale or file.encoding JVM property:可能的原因可能是LC_CTYPE linux 语言环境或file.encoding JVM 属性:

  • Linux LC_CTYPE (or LC_ALL) locale is set to other than UTF-8. Simulated as Linux LC_CTYPE(或 LC_ALL)区域设置为 UTF-8 以外的区域。模拟为

    LC_CTYPE="ISO-8859-1" jshell jshell> import java.nio.charset.Charset; jshell> Charset.defaultCharset() $2 ==> US-ASCII jshell> String a = "adf ł"; a ==> "adf?"
  • JVM has file.encoding property set to other than UTF-8 JVM 的file.encoding属性设置为 UTF-8 以外的值

    jshell -J-Dfile.encoding="ISO-8859-1" --execution="local" jshell> import java.nio.charset.Charset; jshell> Charset.defaultCharset() $2 ==> ISO-8859-1 jshell> String a = "adf ł"; a ==> "adf?" jshell -J-Dfile.encoding="UTF-8" --execution="local" jshell> String a = "adf ł"; a ==> "adf ł"

Linux quick tests Linux 快速测试

java -Dfile.encoding="ISO-8859-1" -XshowSettings:properties -version 2>&1 | grep 'encoding'
    file.encoding = ISO-8859-1
    sun.io.unicode.encoding = UnicodeLittle
    sun.jnu.encoding = UTF-8

java -XshowSettings:properties -version 2>&1 | grep 'encoding'
    file.encoding = UTF-8
    sun.io.unicode.encoding = UnicodeLittle
    sun.jnu.encoding = UTF-8

ps -lf -C jshell | grep 'file.encoding'
0 S lmc      7266  2225 10  80   0 - 895886 -     14:48 pts/2    00:00:05 jshell -J-Dfile.encoding=ISO-8859-1 --execution=local


LC_ALL="es_ES.ISO-8859-1" java -help 2>&1 | tail -n-3
Para especificar un argumento para una opci�n larga, puede usar --<nombre>=<valor> o
--<nombre> <valor>.

Finally the error was in the connection string with DB / JDBC.最后,错误出现在 DB / JDBC 的连接字符串中。

I had to append some params to the connection string URL for Mysql driver:对于 Mysql 驱动程序,我必须将一些参数传递给连接字符串 URL append:

dbConnectionString=jdbc:mysql://localhost/test?useSSL=false&useUnicode=true&characterEncoding=utf-8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM