3.中文乱码问题fix
约 767 字大约 3 分钟
在RestTemplate基础用法博文中,post json表单时,会发现存在中文乱码问题,本文主要介绍对应的解决方案
I. 中文乱码Fix
1. "罪魁祸首"
场景复现
/**
* json表单
*/
public void jsonPost() {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
JSONObject params = new JSONObject();
params.put("name", "一灰灰Blog");
params.put("age", 20);
HttpEntity<String> request = new HttpEntity<>(params.toJSONString(), headers);
String response = restTemplate.postForObject("http://127.0.0.1:8080/body", request, String.class);
log.info("json post res: {}", response);
}
输出结果如下:
(json post res: params: {} | DemoRest.ReqBody(name=???Blog, age=20)
headers: {"content-length":"27","host":"127.0.0.1:8080","content-type":"application/json","connection":"keep-alive","accept":"text/plain, application/json, application/*+json, */*","user-agent":"Java/1.8.0_171"}
cookies:
原因定位
RestTemplate中存在一个HttpMessageConverter
列表的属性成员,而HttpMessageConverter
主要的职责就是消息转码
导致我们中文乱码的一个关键点在于StringHttpMessageConverter
采用的默认编码格式为StandardCharsets.ISO_8859_1
2. 指定StringHttpMessageConverter编码
既然是因为StringHttpMessageConverter
的默认编码不是UTF-8,那么将它手动改成utf-8不就over了么
/**
* 中文乱码问题fix
*/
public void chinese() {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
JSONObject params = new JSONObject();
params.put("name", "一灰灰Blog");
params.put("age", 20);
HttpEntity<String> request = new HttpEntity<>(params.toJSONString(), headers);
// 中文乱码,主要是 StringHttpMessageConverter的默认编码为ISO导致的
List<HttpMessageConverter<?>> list = restTemplate.getMessageConverters();
for (HttpMessageConverter converter : list) {
if (converter instanceof StringHttpMessageConverter) {
((StringHttpMessageConverter) converter).setDefaultCharset(Charset.forName("UTF-8"));
break;
}
}
String response = restTemplate.postForObject("http://127.0.0.1:8080/body", request, String.class);
log.info("json post res: {}", response);
}
测试输出如:
(json post res: params: {} | DemoRest.ReqBody(name=一灰灰Blog, age=20)
headers: {"content-length":"33","host":"127.0.0.1:8080","content-type":"application/json","connection":"keep-alive","accept":"text/plain, application/json, application/*+json, */*","user-agent":"Java/1.8.0_171"}
cookies:
3. 传参POJO
在看RestTemplate
的HttpMessageConvert
时,会看到默认提供了一个MappingJackson2HttpMessageConverter
,那么我们直接传参POJO,走Jackson序列化,是不是也可以解决中文乱码呢?
@Data
@NoArgsConstructor
@AllArgsConstructor
public static class InnerParam implements Serializable {
private static final long serialVersionUID = -3693060057697231136L;
private String name;
private Integer age;
}
// 直接传一个POJO
public void chinese() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
RestTemplate restTemplate = new RestTemplate();
InnerParam innerParam = new InnerParam("一灰灰Blog", 20);
String HttpEntity<InnerParam> entity = new HttpEntity<>(innerParam, headers);
response = restTemplate.postForObject("http://127.0.0.1:8080/body", entity, String.class);
log.info("json post DO res: {}", response);
}
输出结果如下
(json post DO res: params: {} | DemoRest.ReqBody(name=一灰灰Blog, age=20)
headers: {"content-length":"33","host":"127.0.0.1:8080","content-type":"application/json","connection":"keep-alive","accept":"text/plain, application/json, application/*+json, */*","user-agent":"Java/1.8.0_171"}
cookies:
说明:上面的InnerParam对象改成HashMap,也是ok的
II. 其他
0. 项目&系列博文
博文
源码
Loading...