背景:在项目A代码内部,调用项目B的restful接口C,我们采用了RestTemplate进行调用,但是调用过程中,一直不能正常返回数据,日志显示参数存在乱码(有个参数的值是中文)

乱码原因:请求方式是POST,但是我们把参数都放在了url的?后面,参数传递形式与GET请求一样!!!

由于请求方式是POST,所以需要将参数放在body里面进行传递,并且参数需要用MultiValueMap结构体装载,如下所示(RestTemplate的调用改为如下就好了):

方式一:

  if (method == HttpMethod.POST) {
MultiValueMap<String, Object> postParameters = new LinkedMultiValueMap<>();
map.forEach((k, v) -> {
postParameters.add(k, v.toString());
});
return JSON.parseObject(restTemplate.postForObject(url, postParameters, String.class));
}

方式二:

postParam: post请求时body里面的参数

url: 含url后跟的其他参数

 restTemplate.postForObject(url.toString(), new HttpEntity<>(postParam), String.class);

注意,在启动类里加载restTemplate时,需要设置为UTF-8

  @Bean
public RestTemplate getRestTemplate() {
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
return restTemplate;
}

方式三:

适合于 url 后面既有 param 又有 body 的 post 请求

    public void testPostRestTemplate() {
String url = "http://localhost:9999/xxx/xxx";
// url 后面的 param 参数,即 url 问号后面的参数信息
Map<String, Object> urlMap = new HashMap<>(5);
urlMap.put("urlKey1", "urlValue1");
urlMap.put("urlKey2", "urlValue2");
urlMap.put("urlKey3", "urlValue3");
urlMap.put("urlKey4", "urlValue4");
urlMap.put("urlKey5", "urlValue5"); // 将 param 参数追加到 url 后面
StringBuilder sb = new StringBuilder(url);
if (!CollectionUtils.isEmpty(urlMap)) {
sb.append("?");
urlMap.forEach((k, v) -> {
sb.append(k).append("=").append(v).append("&");
});
sb.deleteCharAt(sb.length() - 1);
} // post 请求里面的 body 内容
Map<String, String> bodyMap = new HashMap<>();
bodyMap.put("bodyKey1", "bodyValue1"); // 设置 headers
HttpHeaders httpHeaders = new HttpHeaders();
MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
httpHeaders.setContentType(type); HttpEntity<Map<String, Object>> objectHttpEntity = new HttpEntity(bodyMap, httpHeaders);
ResponseEntity<Object> responseResultResponseEntity = restTemplate.postForEntity(sb.toString(), objectHttpEntity, Object.class);
Object res = responseResultResponseEntity.getBody();
System.out.println(res);
}

使用RestTemplate在代码内调用POST请求的参数乱码问题的更多相关文章

  1. tomcat解决GET请求中文参数乱码

    通常,在使用Spring MVC框架的应用程序中,为了解决中文参数乱码的问题,都会添加如下过滤器配置: <filter> <filter-name>encodingFilter ...

  2. post和get请求的参数乱码

    对于做Java WEB项目同学来说,中文乱码问题是一个经常遇到而又非常头痛的问题,而最容易出现乱码的环节就是在浏览器向服务器发送请求的过程,至于出现乱码的原因不是本文的关注的重点,想了解的朋友可以参考 ...

  3. POST、GET请求中文参数乱码问题

    POST请求中文乱码问题解决方法: 在web.xml文件中添加编码过滤器,如下: <!-- 解决post乱码 --> <filter> <filter-name>C ...

  4. 【java】httpclient的使用之java代码内发送http请求

    场景: 需要本项目发送HTTP请求到另一个项目中,处理完成返回值给本项目. 1.本项目引入架包 <!-- httpclient 后台发送http请求--> <dependency&g ...

  5. 解决Delphi的Indy10组件包TIdHttpServer控件解码HTTP请求时参数乱码的问题

    Delphi6下使用Indy10组件包,其中的TIdHttpServer控件在处理HTTP请求的时候 不能正确解码含有汉字的参数,如: http://127.0.0.1/test?cmd=open&a ...

  6. Tomcat get请求中文参数乱码

    场景:使用Tomcat容器进行get方式传递中文参数到后台乱码. 原因:Tomcat默认的编码方式是ISO--. 解决方案: . 设置cofg-server.xml中的<Connector> ...

  7. java调用Http请求 -HttpURLConnection学习

    最常用的Http请求无非是get和post,get请求可以获取静态页面,也可以把参数放在URL字串后面,传递给servlet,post与get的不同之处在于post的参数不是放在URL字串里面,而是放 ...

  8. spring boot / cloud (八) 使用RestTemplate来构建远程调用服务

    spring boot / cloud (八) 使用RestTemplate来构建远程调用服务 前言 上周因家里突发急事,请假一周,故博客没有正常更新 RestTemplate介绍: RestTemp ...

  9. Spring RestTemplate中几种常见的请求方式

    https://github.com/lenve/SimpleSpringCloud/tree/master/RestTemplate在Spring Cloud中服务的发现与消费一文中,当我们从服务消 ...

随机推荐

  1. enum和数据库entity互转

    注意,code和desc都是string的,数据库的entity是integer,dto的是enum,所以需要一个转换 entity转dto EnumGender.getEnum(String.val ...

  2. operator <<”不明确

    在声明重载的输出运算符<< 时,如果声明时的返回类型是std::ostream,而不是std::ostream&,但是定义的时候又加了引用的话,那么就会报“operator < ...

  3. jsp中如何清除缓存(转)

    <% response.setHeader("Cache-Control","no-cache"); //HTTP 1.1 response.setHea ...

  4. bzoj 2870 最长道路tree——边分治

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2870 关于边分治:https://www.cnblogs.com/Khada-Jhin/p/ ...

  5. windows系统如何设置域名解析

      C:\Windows\System32\drivers\etc        

  6. Windows Azure Virtual Machine (35) Azure VM通过Linked DB,执行SQL Job

    <Windows Azure Platform 系列文章目录> 今天遇到一个Case,客户在使用Azure Automation,执行Azure SQL Database 存储过程的时候, ...

  7. 解决nginx转发websocket报400错误

    解决nginx转发websocket报400错误 说明 由于个人服务器上面有多个项目,配置了二级域名,需要对二级域名进行转发,在转发工作这快采取了大名鼎鼎的nginx.在这之前所有的项目运行转发都没问 ...

  8. P1106删数问题

    链接 题解: #include<iostream>#include<cstdio>#include<cstring>using namespace std;int ...

  9. JVM异常之:方法区溢出OutOfMemoryError: PermGen space

    1.方法区溢出(Perm持久代溢出) 在jdk1.6及之前的版本中,常量池放在Perm区也即是方法区中,所以在jdk1.6版本中,常量池溢出可以说是方法区溢出. 示例一: 方法区溢出的示例见<J ...

  10. checked和stop()的讲解

    input:cheacked (属性选择器): checked 选中复选框 $("p").stop(ture); 代码的翻译:(参数)布尔值  p身上所有的动画都停止了 加不加tr ...