java web乱码_【SpringBoot WEB 系列】RestTemplate 之中文乱码问题 fix

071d96277687df933bcbebdea0818bb4.png

【WEB 系列】RestTemplate 之中文乱码问题 fix

在 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 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 request = new HttpEntity<>(params.toJSONString(), headers);// 中文乱码,主要是 StringHttpMessageConverter的默认编码为ISO导致的
    List> 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

在看RestTemplateHttpMessageConvert时,会看到默认提供了一个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 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. 项目&系列博文

博文

  • 【WEB 系列】RestTemplate 之自定义请求头
  • 【WEB 系列】RestTemplate 基础用法小结

源码

  • 工程:https://github.com/liuyueyi/spring-boot-demo
  • 源码: https://github.com/liuyueyi/spring-boot-demo/tree/master/spring-boot/221-web-resttemplate

1. 一灰灰 Blog

尽信书则不如,以上内容,纯属一家之言,因个人能力有限,难免有疏漏和错误之处,如发现 bug 或者有更好的建议,欢迎批评指正,不吝感激

下面一灰灰的个人博客,记录所有学习和工作中的博文,欢迎大家前去逛逛

  • 一灰灰 Blog 个人博客 https://blog.hhui.top
  • 一灰灰 Blog-Spring 专题博客 http://spring.hhui.top
2a9d0afda19ddd2c604b2e8f8f402a8a.png
一灰灰blog
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值