简体   繁体   English

Spring Boot编码问题是ISO而不是UTF-8

[英]Spring Boot encoding issue ISO instead of UTF-8

Spring Boot is decoding a String from a @RequestHeader annotation with ISO-8859-1 instead of UTF-8 . Spring Boot正在使用ISO-8859-1而不是UTF-8@RequestHeader注释中解码字符串。

abstract of application.properties application.properties的摘要

spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
spring.http.encoding.force-request=true
spring.http.encoding.force-response=true
spring.messages.encoding=UTF-8
spring.banner.charset=UTF-8
spring.mandatory-file-encoding=UTF-8
server.tomcat.uri-encoding=UTF-8
spring.freemarker.charset=UTF-8

Controller 调节器

@Controller
@RequestMapping(value = "/mapping/", produces = "text/plain;charset=UTF-8")
public class MyController {}

curl command curl命令

curl 'https://example.com/mymapping/' -H 'Connection: keep-alive' -H 'Cache-Control: max-age=0' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8' -H 'Referer: https://example.com/mymapping/' -H 'Accept-Encoding: gzip, deflate, br' -H 'Accept-Language: en-US,en;q=0.9,de;q=0.8' -H 'Cookie: cookiesession1=<*>; mellon-cookie=<*>; JSESSIONID=<*>' --compressed

Some special characters are still wrong decoded for example a ü will be ü afterwards. 一些特殊字符还是错的解码例如üü之后。

With the following workaround I get the correct string. 通过以下变通办法,我可以获得正确的字符串。

private static final Charset UTF_8 = Charset.forName("UTF-8");
private static final Charset ISO = Charset.forName("ISO-8859-1");

String textnew = new String(textold.getBytes(ISO), UTF_8);

I want to declare my application to decode all data in UTF-8 not ISO or something similiar. 我想声明我的应用程序以UTF-8而非ISO或类似格式解码所有数据。 Any suggestions? 有什么建议么?

Can you please verify the response headers what is the value for Content-Type:? 您能否验证响应标头的Content-Type:值是什么? for ex :application/json;charset=UTF-8 对于ex:application / json; charset = UTF-8

我读过Java的某个地方期望* .properties文件是在ISO-8859-1中编码的,这就是为什么Spring会将application.properties视为在ISO-8859-1中的原因。请尝试使用YAML而不是属性文件。

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

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