RestTemplate 发送 get 请求使用误区 多值为null
来源:互联网 发布:怎么跟淘宝客服讲价 编辑:程序博客网 时间:2024/06/08 23:50
首先看一下官方文档是怎么描述的,传递多个值的情况(注意例子中用到的@pathParam,一般要用@queryParam)
RestTemplate 实例
@Configurationpublic 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=短信内容
错误使用
@Autowiredprivate 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);}
服务器接收的时候你会发现,接收的该请求时没有参数的
正确使用
@Autowiredprivate 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);}
等价于
@Autowiredprivate 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", "测试短信内容");}
0 0
- RestTemplate 发送 get 请求使用误区 多值为null
- RestTemplate 发送 get 请求使用误区 多值为null
- spring boot RestTemplate 发送 get 请求使用误区
- 使用RestTemplate发送post请求
- 使用RestTemplate发送post和delete请求
- Spring RestTemplate get方式发送数据服务器端拿到为空
- 使用HttpClient发送GET请求
- 使用c发送GET请求
- 使用httpclient发送get请求
- 以json为body发送get请求
- android手机使用ksoap2发送soap请求,服务端接收参数为null
- RestTemplate 发送json格式的http请求
- RestTemplate发送请求并携带header信息
- Spring框架中发送http请求--RestTemplate
- 关于使用NSURLConnection发送get请求响应为nil的情况
- 使用HttpURLConnection发送post和get请求
- 使用Java发送GET、POST请求
- 使用Java发送POST、GET请求【转】
- Gradle build.gradle样例文件 Groovy语法 注解
- Profile-多环境配置
- 51单片机 软件模拟IIC
- 崇志宏 【转载】深度学习进阶规划(论文阅读顺序推荐)--东南大学
- 【day-14】二叉树-剑指offer难题合集
- RestTemplate 发送 get 请求使用误区 多值为null
- openresty 开发入门
- #Hello CSDN
- grep命令以及cut命令的使用
- 数据结构与算法分析 2.23 不用递归,写出快速求幂的程序
- eclipse在debug时候,可能鼠标放上去,有些变量不直接显示咋办?
- Scrum敏捷开发流程
- nginx的多阶段请求处理
- hdu1573 X问题(中国剩余定理 不互质)