簡體   English   中英

Spring Boot編碼問題是ISO而不是UTF-8

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

Spring Boot正在使用ISO-8859-1而不是UTF-8@RequestHeader注釋中解碼字符串。

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
@RequestMapping(value = "/mapping/", produces = "text/plain;charset=UTF-8")
public class MyController {}

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

一些特殊字符還是錯的解碼例如üü之后。

通過以下變通辦法,我可以獲得正確的字符串。

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);

我想聲明我的應用程序以UTF-8而非ISO或類似格式解碼所有數據。 有什么建議么?

您能否驗證響應標頭的Content-Type:值是什么? 對於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