springboot 注入 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", "测试短信内容");
}
springboot 注入 restTemplate的更多相关文章
- Spring Boot注入RestTemplate ,出现空指针解决办法
SpringBoot 注入RestTemplate 我看了一下大都是让我们在启动类里面加一个Bean配置代码如下 @Autowired private RestTemplateBuilder buil ...
- SpringBoot系列: RestTemplate 快速入门
====================================相关的文章====================================SpringBoot系列: 与Spring R ...
- Springboot 使用 RestTemplate
每天学习一点点 编程PDF电子书.视频教程免费下载:http://www.shitanlife.com/code spring web 项目提供的RestTemplate,使java访问url更方便, ...
- @Autowired中无法注入RestTemplate的问题
1.在启动类中添加 @Beanpublic RestTemplate restTemplate(){ return new RestTemplate();} 即可解决无法注入RestTemplate的 ...
- springboot管理类,springboot注入类
springboot管理类,springboot注入类 定义一个配置类,添加@Configuration注解,EvaluatorTemplate代表你需要注入的第三方类 @Configuration ...
- SpringBoot注入Mapper失败
SpringBoot注入Mapper失败,可能是因为没有加扫描Mapper层的注解 方式一:在所有mapper接口使用@Mapper注解 @Mapper public interface UserMa ...
- SpringBoot 使用 RestTemplate 调用exchange方法 显示错误信息
SpringBoot使用RestTempate SpringBoot使用RestTemplate摘要认证 SpringBoot使用RestTemplate基础认证 SpringBoot使用RestTe ...
- SpringBoot使用RestTemplate基础认证
SpringBoot使用RestTempate SpringBoot使用RestTemplate摘要认证 SpringBoot使用RestTemplate基础认证 SpringBoot使用RestTe ...
- SpringBoot使用RestTemplate 摘要认证
SpringBoot使用RestTempate SpringBoot使用RestTemplate摘要认证 SpringBoot使用RestTemplate基础认证 SpringBoot使用RestTe ...
随机推荐
- span 超出内容自动换行
<span style="width:80%;word-break:normal;display:block;word-warp:break-word;overflow:hidden; ...
- CSUOJ 2031 Barareh on Fire
Description The Barareh village is on fire due to the attack of the virtual enemy. Several places ar ...
- JavaScript 网页脚本语言 由浅入深 (随笔)
1)基础 学习目的: 1. 客户端表单验证 2. 页面动态效果 3. jQuery的基础 什么是JavaScript? 一种描述性语言,也是一种基于对象和事件驱动的,并具有安全性能的脚本语言 java ...
- ServletConfig对象和ServletContext对象
(1)ServletConfig:用来保存一个Servlet的配置信息的(比如 : name, class, url ... ) 这些配置信息没什么大用处,我们还可以在ServletConfig中保存 ...
- UNP学习总结(二)
本文是UNP复习系列的第二篇,主要包括了以下几个内容 UNIX系统下5种I/O模型 阻塞.非阻塞,同步.异步 epoll函数用例 一.Unix下的五种可用I/O模型 阻塞式I/O模型 阻塞式I/O是最 ...
- wpf企业应用之主从结构列表
主从结构在企业级应用中相当常见,这里结合我的例子谈一下wpf中主从结构列表展示的常用做法,具体效果见 wpf企业级开发中的几种常见业务场景. 首先,Model有两种,主表对应model(假设为mode ...
- SPOJ6717 Two Paths 树形dp
首先有朴素的\(O(n^2)\)想法 首先枚举断边,之后对于断边之后的两棵子树求出直径 考虑优化这个朴素的想法 考虑换根\(dp\) 具体而言,首先求出\(f[i], fs[i]\)表示\(i\)号点 ...
- disable_functions php-fpm root
php.ini disable_functions 禁用某些函数 需要时注意打开 php-fpm 对应conf user group为root时 ERROR: [pool www] please sp ...
- 【assembly】用汇编写的一个BMP图片读取器
;----------------------------- ;文件满足256色调的 ;----------------------------- Stack Segment ...
- [COGS2580]偏序 II
[COGS2580]偏序 II 题目大意: \(n(n\le50000)\)个五元组,求五维偏序. 思路: CDQ分治套CDQ分治套CDQ分治套树状数组. 时间复杂度\(\mathcal O(n\lo ...