http://blog.csdn.net/zhousenshan/article/details/71055687

*****************************************************

首先看一下官方文档是怎么描述的,传递多个值的情况(注意例子中用到的@pathParam,一般要用@queryParam)

RestTemplate 实例

guration
public class RestConfiguration { @Bean
@ConditionalOnMissingBean({RestOperations.class, RestTemplate.class})
public RestOperations restOperations() {
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setReadTimeout(5000);
requestFactory.setConnectTimeout(5000); RestTemplate restTemplate = new RestTemplate(requestFactory); // 使用 utf-8 编码集的 conver 替换默认的 conver(默认的 string conver 的编码集为 "ISO-8859-1")
List<HttpMessageConverter<?>> messageConverters = restTemplate.getMessageConverters();
Iterator<HttpMessageConverter<?>> iterator = messageConverters.iterator();
while (iterator.hasNext()) {
HttpMessageConverter<?> converter = iterator.next();
if (converter instanceof StringHttpMessageConverter) {
iterator.remove();
}
}
messageConverters.add(new StringHttpMessageConverter(Charset.forName("UTF-8"))); return restTemplate;

请求地址

get 请求 url 为

/localhost:8080/test/sendSms?phone=手机号&msg=短信内容

错误使用

ired
private RestOperations restOperations; public void test() throws Exception{
String url = "http://localhost:8080/test/sendSms"; Map<String, Object> uriVariables = new HashMap<String, Object>();
uriVariables.put("phone", "151xxxxxxxx");
uriVariables.put("msg", "测试短信内容"); String result = restOperations.getForObject(url, String.class, uriVariables);

服务器接收的时候你会发现,接收的该请求时没有参数的


正确使用

ired
private RestOperations restOperations; public void test() throws Exception{
String url = "http://localhost:8080/test/sendSms?phone={phone}&msg={phone}"; Map<String, Object> uriVariables = new HashMap<String, Object>();
uriVariables.put("phone", "151xxxxxxxx");
uriVariables.put("msg", "测试短信内容"); String result = restOperations.getForObject(url, String.class, uriVariables);

等价于

ired
private RestOperations restOperations; public void test() throws Exception{
String url = "http://localhost:8080/test/sendSms?phone={phone}&msg={phone}"; String result = restOperations.getForObject(url, String.class, "151xxxxxxxx", "测试短信内容");

RestTemplate 发送 get 请求使用误区 多值为null的更多相关文章

  1. RestTemplate 发送 get 请求使用误区 多个参数传值为null(转载)

    首先看一下官方文档是怎么描述的,传递多个值的情况(注意例子中用到的@pathParam,一般要用@queryParam) RestTemplate 实例 @Configuration public c ...

  2. 使用RestTemplate发送post请求

    最近使用RestTemplate发送post请求,遇到了很多问题,如转换httpMessage失败,中文乱码等,调了好久才找到下面较为简便的方法: RestTemplate restTemplate ...

  3. 使用RestTemplate发送post请求,请求头中封装参数

    最近使用RestTemplate发送post请求,遇到了很多问题,如转换httpMessage失败.中文乱码等,调了好久才找到下面较为简便的方法: RestTemplate restTemplate ...

  4. RestTemplate 发送post请求

    springboot使用restTemplate post提交值 restTemplate post值 post提交有 FormData和Payload 两种形式: 第一种是formdata形式,在h ...

  5. ajax post请求request.getParameter("")取值为null

    今天在写提交一个json数据到后台,然后后台返回一个json数据类型.但是发现后台通过request.getParamter("")取到的值为null. 于是写一个简单的ajax ...

  6. 使用restTemplate发送post请求,传入参数是在requestBody请求体中,以json形式传输

    @PostMapping public ResponseResult add(User user){ HttpHeaders httpHeaders = new HttpHeaders(); Medi ...

  7. RestTemplate发送GET请求

    import org.springframework.web.client.RestTemplate; @Component @Slf4j public class JsSdkUtil { /** * ...

  8. vue axios 发送post请求,后端接收参数为null

    1首先检查自己的传参方式是否正确,我是传一个对象,没有问题,接口也触发了 2查了下资料说是 Content-Type的问题,设置为   'application/x-www-form-urlencod ...

  9. JAVA使用apache http组件发送POST请求

    在上一篇文章中,使用了JDK中原始的HttpURLConnection向指定URL发送POST请求 可以看到编码量有些大,并且使用了输入输出流 传递的参数还是用“name=XXX”这种硬编的字符串进行 ...

随机推荐

  1. LLVM和clang

    LLVM编译器架构 LLVM项目是一套工具的集合,它包括模块化.可复用的编译器及一些列工具链技术. LLVM最开始是Low Level Virtual Machine的简称,但现在它并不是传统意义上的 ...

  2. 如何从windows中拷贝文件到linux (ubuntu)??

    安装ssh,然后用客户端似的界面远程手动拖动即可.

  3. PCRE函数简介和使用示例【转】

    PCRE函数简介和使用示例 标签: 正则表达式listbuffercompilationnullperl 原文地址:http://blog.csdn.net/sulliy/article/detail ...

  4. 看过这两张图,就明白 Buffer 和 Cache 之间区别

    Buffer常见的是这个: 对,就是铁道端头那个巨大的弹簧一类的东西.作用是万一车没停住,撞弹簧上减速慢,危险小一些.叫缓冲. Cache常见的是这个: 没错,就是一种保管箱.看到右边那个被锈掉的Fo ...

  5. Linux操作系统中文件结构stat中st_size的说明以及对于文件中洞(Holes)的理解

    文件stat结构体中st_size成员 对于所有的文件类型,st_size成员对其中的普通文件.目录以及符号链接有实在的意义.其中,对于普通文件而言,st_size记录了该文件的实际大小:对于目录而言 ...

  6. kubernetes删除pod失败

    一.概述 k8s中删除pod失败,可能是该pod有rc,rs上层控制,而且很有可能,所以删除上层对应的rc,rs,deployment即可: 删除的方法: 1.直接删除rc,rs,deployment ...

  7. 【java】解析java中的数组

    目录结构: contents structure [+] 一维数组 1,什么是一维数组 2,声明一维数组的三种方式 二维数组 1,什么是二维数组 2,声明二维数组的3种方式 3,二维数组的遍历示例 数 ...

  8. 日志收集-Flume-ng-mongodb-sink

    本文主要介绍使用Flume传输数据到MongoDB的过程,内容涉及环境部署和注意事项. 一.环境搭建 1.flune-ng下载地址:http://www.apache.org/dyn/closer.c ...

  9. mysql使用GROUP BY分组实现取前N条记录的方法

    MySQL中GROUP BY分组取前N条记录实现 mysql分组,取记录 GROUP BY之后如何取每组的前两位下面我来讲述mysql中GROUP BY分组取前N条记录实现方法. 这是测试表(也不知道 ...

  10. 获取*.jks签名的方法(Android studio)