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

RestTemplate 实例

@Configuration
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 为

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

错误使用

@Autowired
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);
}

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


正确使用

@Autowired
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);
}

等价于

@Autowired
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

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

  2. 使用RestTemplate发送post请求

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

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

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

  4. JQuery发送ajax请求不能用数组作为参数

    JQuery发送ajax请求不能用数组作为参数,否则会接收不到参数, 一.js代码如下: $('#delete-button').click(function(){        var select ...

  5. RestTemplate 发送post请求

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

  6. JAVA发送POST请求携带JSON格式字符串参数

    import org.apache.commons.lang.StringUtils; import org.apache.http.HttpEntity; import org.apache.htt ...

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

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

  8. RestTemplate发送GET请求

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

  9. jenkins构建自动执行jmeter 发送http请求,中间有替换参数路径

    #在构建目录下创建jmeter目录,在这个目录下面执行jmeter性能测试mkdir -p $WORKSPACE/target/apache-jmeter-3.1/#复制jmeter文件到执行测试目录 ...

随机推荐

  1. netdom remove 错误:netdom remove

    自己用错了命令,直接将加入域的计算机使用dsrm删除了,本来应该使用netdom remove的,结果在域控制器上使用netdom remove错误,在客户端上登录时一样提示:netdom remov ...

  2. .Net Core 在Linux服务器下部署程序--(2). 部署前需要安装的软件及SDK

    安装通用软件 : ping , telnet , lrzsz , zip , wget 安装ping软件,用于测试当前服务器与其他服务器之间的网络通畅情况,和Windows的Ping类似 执行ping ...

  3. [Inside HotSpot] 模板解释器

    0. 简介 众所周知,hotspot默认使用解释+编译混合(-Xmixed)的方式执行代码.它首先使用模板解释器对字节码进行解释,当发现一段代码是热点的时候,就使用C1/C2 JIT进行优化编译再执行 ...

  4. chrome谷歌浏览器开发者工具-网络带宽控制

    有时候我们想在本地测试一下图片预加载loading的加载情况,如下图: 可无奈一般网络带宽都比较好,基本上看不到效果,图片一下子就加载出来了, 可能这个时候有些小伙伴想到的办法是用定时器延迟加载 其实 ...

  5. 图解 -- Win10 OpenSSH

    一.安装OpenSSH 客户端 .OpenSSH 服务器 设置 -> 管理可选功能 -> 添加功能 -> [OpenSSH 客户端]   [OpenSSH 服务器]  1.设置 2. ...

  6. SpringBoot基础系列一

    SpringBoot基础知识概览 特性 核心理念:约定优于配置 特点: 1. 开箱即用,根据项目依赖自动配置 2. 功能强大的服务体系,如嵌入式服务.安全 3. 绝无代码生成,不用写.xml配置,用注 ...

  7. 用ASP.NET Core 2.1 建立规范的 REST API -- 翻页/排序/过滤等

    本文所需的一些预备知识可以看这里: http://www.cnblogs.com/cgzl/p/9010978.html 和 http://www.cnblogs.com/cgzl/p/9019314 ...

  8. Identity Server 4 预备知识 -- OpenID Connect 简介

    我之前的文章简单的介绍了OAuth 2.0 (在这里: https://www.cnblogs.com/cgzl/p/9221488.html), 还不是很全. 这篇文章我要介绍一下 OpenID C ...

  9. 架构师入门:Spring Cloud系列,Hystrix与Eureka的整合

    和Ribbon等组件一样,在项目中,Hystrix一般不会单独出现,而是会和Eureka等组件配套出现.在Hystrix和Eureka整合后的框架里,一般会用到Hystrix的断路器以及合并请求等特性 ...

  10. 【重学计算机】机组D8章:输入输出系统

    1. 输入输出系统概述 组成:外设.接口.总线.管理软件 基本功能 完成计算机内外的信息传递 保证CPU正确选择输出设备 利用缓冲等,实现主机与外设的速度匹配 特点:异步性.实时性.设备无关性 输入过 ...