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 ...
随机推荐
- docker 删除none镜像
Windows版本,参考官方地址:https://forums.docker.com/t/how-to-remove-none-images-after-building/7050 docker rm ...
- 30:最小的K个数
import java.util.ArrayList; import java.util.TreeSet; /** * 面试题30:最小的K个数 * 输入n个整数,找出其中最小的K个数.例如输入4,5 ...
- thinkphp3.2局部不缓存的静态缓存
在thinkphp中,对于访问量大的网站可以用静态缓存来越过数据库瓶颈来提高访问速度,但有时候并不是整个页面都要缓存的,如登录的用户名部分,那么如何实现局部不缓存呢? 其实有多种方法,但对于Think ...
- thinkphp数据查询方法总结select ,find,getField,query
thinkphp已经封装好了常用的查询方法,且都比较实用,对于不常用的查询框架也保留了原始查询方法query. 1 2 $Model = new Model() // 实例化一个model对象 没有对 ...
- CI框架的事务开启、提交和回滚
1.运行事务 $this->db->trans_start(); // 开启事务$this->db->query('一条SQL查询...');$this->db-> ...
- 理解URI
---恢复内容开始--- 参考 https://zh.wikipedia.org/wiki/%E7%BB%9F%E4%B8%80%E8%B5%84%E6%BA%90%E6%A0%87%E5%BF%97 ...
- Highmaps网页图表教程之图表配置项结构与商业授权
Highmaps网页图表教程之图表配置项结构与商业授权 Highmaps图表配置项结构 Highmaps最核心的部分就是图表配置项.用户通过图表配置项来对标题进行定制,从而实现自己所要的效果.所以,掌 ...
- OpenStack计费项目Cloudkitty系列详解(一)
云计算是一种按需付费的服务模式,虽然OpenStack前期在计量方面走了些“弯路”,但现在的ceilometer.gnocchi.aodh.panko项目的稳步并进算是让其峰回路转.然而,目前来看Op ...
- linux 设备文件和设备之间联系的建立
<设备驱动模型> 注:几乎所有的设备结构体都包含"strcut kobject kobj"和"srtuct list_head list"该结构体 ...
- [luogu4389]付公主的背包(多项式exp)
完全背包方案计数问题的FFT优化.首先写成生成函数的形式:对重量为V的背包,它的生成函数为$\sum\limits_{i=0}^{+\infty}x^{Vi}=\frac{1}{1-x^{V}}$于是 ...