SpringBoot非官方教程 | 第十六篇:用restTemplate消费服务
转载请标明出处:
原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot11-restTemplate/
本文出自方志朋的博客
这篇文章主要介绍怎么用消费一个 Restful的web服务。我将用restTemplate去消费一个服务: http://gturnquist-quoters.cfapps.io/api/random.
构架工程
创建一个springboot工程,去消费RESTFUL的服务。这个服务是 http:///gturnquist-quoters.cfapps.io/api/random ,它会随机返回Json字符串。
在Spring项目中,它提供了一个非常简便的类,叫RestTemplate,它可以很简便的消费服务。
消费服务
通过RestTemplate消费服务,需要先context中注册一个RestTemplate bean。代码如下:
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
@Bean
public CommandLineRunner run(RestTemplate restTemplate) throws Exception {
return args -> {
String quote = restTemplate.getForObject(
"http://gturnquist-quoters.cfapps.io/api/random", String.class);
log.info(quote.toString());
};
}
运行程序,控制台打印:
{
“type”: “success”,
“value”: {
“id”: 6,
“quote”: “It embraces convention over configuration, providing an experience on par with frameworks that excel at early stage development, such as Ruby on Rails.”
}
}
参考资料
https://spring.io/guides/gs/consuming-rest/
源码下载
https://github.com/forezp/SpringBootLearning
扫码关注公众号有惊喜
(转载本站文章请注明作者和出处 方志朋的博客)
SpringBoot非官方教程 | 第十六篇:用restTemplate消费服务的更多相关文章
- SpringBoot非官方教程 | 第二十六篇: sprinboot整合elk,搭建实时日志平台
转载请标明出处: 原文首发于https://www.fangzhipeng.com/springboot/2017/07/11/sprinboot25-elk/ 本文出自方志朋的博客 这篇文章主要介绍 ...
- (转) SpringBoot非官方教程 | 第二十四篇: springboot整合docker
这篇文篇介绍,怎么为 springboot程序构建一个Docker镜像.docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从Apache2.0协议开源.Docker 可以让开发者打包他们的 ...
- SpringBoot非官方教程 | 第二十四篇: springboot整合docker
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot24-docker/ 本文出自方志朋的博客 这篇文 ...
- SpringBoot非官方教程 | 第二十二篇: 创建含有多module的springboot工程
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springbot22-modules/ 本文出自方志朋的博客 这篇文 ...
- SpringBoot非官方教程 | 第十九篇: 验证表单信息
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot19/ 本文出自方志朋的博客 这篇文篇主要简述如何 ...
- SpringBoot非官方教程 | 第十八篇: 定时任务(Scheduling Tasks)
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot18-scheduling/ 本文出自方志朋的博客 ...
- SpringBoot非官方教程 | 第十五篇:Springboot整合RabbitMQ
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot15-rabbitmq/ 本文出自方志朋的博客 这 ...
- SpringBoot非官方教程 | 第十四篇:在springboot中用redis实现消息队列
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot14-redis-mq/ 本文出自方志朋的博客 这 ...
- SpringBoot非官方教程 | 第二十五篇:2小时学会springboot
转载请标明出处: http://blog.csdn.net/forezp/article/details/61472783 本文出自方志朋的博客 一.什么是spring boot Takes an o ...
随机推荐
- [转]Implementing User Authentication in ASP.NET MVC 6
本文转自:http://www.dotnetcurry.com/aspnet-mvc/1229/user-authentication-aspnet-mvc-6-identity In this ar ...
- EFCodeFirst 各种命令整理
1.Enable-Migrations (创建迁移目录:Migrations,如果有多个数据上下文可以用 -ContextTypeName 命令迁移对应的数据上下文 ) 2.Add-Migratio ...
- python搭建本地服务器
python搭建本地服务器 python3以上版本 'python3 -m http.server 8000' 默认是8000端口,可以指定端口,打开浏览器输入http://127.0.0.1:800 ...
- ubuntu 14.04 64bit 安装 oracle 11g r2
参考文章:http://tutorialforlinux.com/2016/03/09/how-to-install-oracle-11g-r2-database-on-ubuntu-14-04-tr ...
- 新手的grid布局
html部分 <!DOCTYPE html><html><head><meta charset="utf-8"><link r ...
- 使用jquery去掉时光轴头尾部的线条
一.前言:以前做类似时光轴的结构,几乎都是一条灰色线飞流直下,没有尽头.今天这个线条是从第一个圆点到最后一个圆点,那么问题来了,内容的高度还不是固定的,线条的长度怎么确定?怎么就能刚刚好从第一个点到最 ...
- 菜鸟学习Spring——SpringMVC注解版在服务器端获取Json字符串并解析
一.概述. SpringMVC在服务端把客户端传过来的JSON字符串,并把JSON字符串转成 JSON对象并取得其中的属性值,这个在项目中经常用到. 二.代码演示. 需要添加的jar包. 2.1 we ...
- 打杂程序员之nginx服务配置
现在公司要在服务器上多加个网站用同一个nginx服务器,而且都是公用80端口. 因为服务器上跑着好几个网站了,所以配置文件配置完成时候要检测一下能不能用,用nginx -t:最好不要直接杀死nginx ...
- web.xml中不同版本的servlet头以及版本控制
参考文章: http://www.cnblogs.com/beijingstruggle/p/5461146.html http://blog.csdn.net/z69183787/article/d ...
- asyncio标准库2 Hello Clock
如何调度协程,并发运行 asyncio.gather方法可以聚合协程or future def gather(*coros_or_futures, loop=None, return_exceptio ...