RestTemplate 发送 get 请求使用误区 多值为null
2017-09-27 09:55
363 查看
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
- RestTemplate 发送 get 请求使用误区 多值为null
- spring boot RestTemplate 发送 get 请求使用误区
- PHP发送POST请求的三种方式 分别使用curl file_get_content fsocket 来实现post提交数据
- Android HTTP实例 使用GET方法和POST方法发送请求
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- php使用fsockopen函数发送post,get请求获取网页内容的方法
- 如何使用SOCKET 发送HTTP1.1 GET POST请求包
- java 中使用URL发送get,post请求乱码问题
- C/C++使用libcurl库发送http请求(get和post可以用于请求html信息,也可以请求xml和json等串)
- PHP 使用curl库来发送GET,POST请求,处理json格式数据
- 发送GET和POST请求(使用NSURLSession)
- 使用Java发送POST、GET请求【转】
- ajax之使用get方式向服务器发送请求
- 使用HttpClient 发送 GET、POST、PUT、Delete请求及文件上传
- C++网络编程 如何使用SOCKET 发送HTTP1.1 GET POST请求包
- 使用HttpURLConnection向服务器发送post和get请求
- Linux下使用curl命令发送GET请求时多个参数截断的问题
- Python -- 使用requests模块发送GET和POST请求
- 如何使用SOCKET 发送HTTP1.1 GET POST请求包