http://xiangai.taobao.com
http://shop148612228.taobao.com

restTemplate 的post请求解决 乱码问题 设置UTF-8

HttpHeaders headers = new HttpHeaders();
MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
// 请勿轻易改变此提交方式,大部分的情况下,提交方式都是表单提交
headers.setContentType(type);
headers.add("Accept", MediaType.APPLICATION_JSON.toString());
JSONObject jsonObj = new JSONObject();
jsonObj.put("uid",sendMessage.getUId());
jsonObj.put("message",sendMessage.getMessage());
jsonObj.put("param",sendMessage.getParam());
// 封装参数,千万不要替换为Map与HashMap,否则参数无法传递
MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();

HttpEntity<String> requestEntity = new HttpEntity<String>(jsonObj.toString(), headers);
// 执行HTTP请求
ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);

posted @ 2021-02-09 09:50  万事俱备就差个程序员  阅读(4681)  评论(0编辑  收藏  举报

http://xiangai.taobao.com
http://shop148612228.taobao.com
如果您觉得对您有帮助.领个红包吧.谢谢.
支付宝红包
微信打赏 支付宝打赏