spring boot RestTemplate 发送 get 请求使用误区
2016-12-31 16:06
811 查看
闲话少说,代码说话
RestTemplate 实例
手动实例化,这个我基本不用RestTemplate restTemplate = new RestTemplate();
依赖注入,通常情况下我使用 java.net 包下的类构建的 SimpleClientHttpRequestFactory
@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", "测试短信内容"); }
相关文章推荐
- spring boot RestTemplate 发送 get 请求
- Spring boot 发送http请求组件RestTemplate使用实例
- RestTemplate 发送 get 请求使用误区 多值为null
- 解决spring3.0.5使用RestTemplate发送post请求响应中文乱码问题
- spring boot 使用@RestController 和 @Controller 构造常见http get ,post请求例子
- springboot使用restTemplate post提交值 restTemplate post值
- Spring RestTemplate get方式发送数据服务器端拿到为空
- java后端发送http请求使用RestTemplate(简单的都无敌了)
- RestTemplate 发送 get 请求使用误区 多值为null
- 使用 RestTemplate 发送 post 请求后返回 json 中 ":" 变成 "=" 的解决办法
- springboot-1-restTemplate的使用
- springboot restTemplate使用以及相关配置
- 使用RestTemplate发送post请求
- 使用 Spring RestTemplate 调用 rest 服务时自定义请求头(custom HTTP headers)
- 使用RestTemplate消费spring boot的Restful服务
- springboot-24-restTemplate的使用
- SpringBoot系列十一:SpringBoot整合Restful架构(使用 RestTemplate 模版实现 Rest 服务调用、Swagger 集成、动态修改日志级别)
- 在spring boot中使用restTemplate上传大文件Java heap space
- spring boot / cloud (八) 使用RestTemplate来构建远程调用服务
- springboot中配置restTemplate及简单使用