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

时间:2019-03-04 12:18:07

标签: java spring-boot encoding

Spring Boot正在使用@RequestHeader而非ISO-8859-1UTF-8批注中解码字符串。

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 '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或类似的东西)进行解码。有什么建议吗?

2 个答案:

答案 0 :(得分:0)

能否请您确认响应标头的Content-Type:值是什么?  例如::application / json; charset = UTF-8

答案 1 :(得分:0)

我读过Java的某个地方希望* .properties文件是在ISO-8859-1中编码的,这就是为什么Spring会将application.properties当作在ISO-8859-1中一样对待。尝试使用YAML而不是属性文件。