问题描述:使用SpringBoot发送HTTP请求时,请求参数乱码。本来应该是中文的内容却解析成了???
在本地环境中解析都是正常的,但是在发布环境解析却出了问题,百思不得其解。
问题排查:发现是发布环境中默认的字符集不是UTF-8,虽然SpringBoot默认的环境中使用的是UTF-8字符集,但是真正通过SpringBoot去发送HTTP请求的时候却使用了系统环境的字符集。
解决方案:在SpringBoot启动时修改系统默认的字符集为UTF-8
public static void main(String[] args) {
// 修改系统默认字符集,解决url解析中文乱码问题
if (! "UTF-8".equalsIgnoreCase(System.getProperty("file.encoding"))) {
System.setProperty("file.encoding", StandardCharsets.UTF_8.name());
}
SpringApplication.run(Application.class, args);
}