首先看一下官方文档是怎么描述的,传递多个值的情况(注意例子中用到的@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. Windows Server 2016-图形化新建域用户(一)

    上章节我们介绍了有关OU组织单位的日常管理,本章我们将对域用户的创建进行简单介绍,常规的操作方法是通过管理控制台图形化手工创建,具体操作方法如下: 1.常规管理控制台 Active Directory ...

  2. C语言多线程的一个简单例子

    多线程的一个简单例子: #include <stdio.h> #include <stdlib.h> #include <string.h> #include &l ...

  3. Activity 之生命周期

    Activity 之生命周期 本文内容: 1. Activity 介绍 2. Activity 的生命周期 2.1 生命周期图 2.2 常见情况下生命周期的回调 2.3 关于生命周期常见问题 2.4 ...

  4. 【activity任务栈】浅析

    背景知识 Activity四种启动模式 标准模式 standard 栈顶复用模式 singleTop 栈内复用模式 singleTask 单例模式 singleInstance 任务栈 Activit ...

  5. springcloud~Eureka实例搭建

    服务端 build.gradle配置 dependencies { compile('org.springframework.cloud:spring-cloud-starter-netflix-eu ...

  6. Condition条件变量

    条件变量是一种比较复杂的线程同步机制 #!/usr/bin/env python # -*- coding: utf-8 -*- """ 条件变量,线程间通信提供的另一种 ...

  7. Shell从入门到精通进阶之四:流程控制

    流程控制是改变程序运行顺序的指令. 4.1 if语句 4.1.1 单分支 if 条件表达式; then 命令 fi 示例: #!/bin/bash N=10 if [ $N -gt 5 ]; then ...

  8. Python编程从入门到实践笔记——列表简介

    Python编程从入门到实践笔记——列表简介 #coding=utf-8 #列表——我的理解等于C语言和Java中的数组 bicycles = ["trek","cann ...

  9. electron开发客户端注意事项(兼开源个人知识管理工具“想学吗”)

    窗口间通信的问题 electron窗口通信比nwjs要麻烦的多 electron分主进程和渲染进程,渲染进程又分主窗口的渲染进程和子窗口的渲染进程 主窗口的渲染进程给子窗口的渲染进程发消息 subWi ...

  10. [SpringBoot guides系列翻译]调度任务

    原文 调度任务 用spring实现一个任务调度. 你将做的 你将做一个应用每5秒钟打印当前时间,用@Scheduled注解. 你需要啥 15分钟 文本编辑器或者IDE JDK1.8+ Gradle4+ ...