1. 使用RestTemplate做这件事非常简单
  1. package hello;
  2.  
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.boot.CommandLineRunner;
  6. import org.springframework.boot.SpringApplication;
  7. import org.springframework.boot.autoconfigure.SpringBootApplication;
  8. import org.springframework.web.client.RestTemplate;
  9.  
  10. @SpringBootApplication
  11. public class Application implements CommandLineRunner {
  12.  
  13. private static final Logger log = LoggerFactory.getLogger(Application.class);
  14.  
  15. public static void main(String args[]) {
  16. SpringApplication.run(Application.class);
  17. }
  18.  
  19. @Override
  20. public void run(String... args) throws Exception {
  21. RestTemplate restTemplate = new RestTemplate();
  22. Quote quote = restTemplate.getForObject("http://gturnquist-quoters.cfapps.io/api/random", Quote.class);
  23. log.info(quote.toString());
  24. }
  25. }

在日常的工作中我们返回的结果通常是一个Wrapper,含有一个泛型属性来表示真正的业务对象。这就要用到ParameterizedTypeReference

代码就成了这样了

  1. @Override
  2. public void run(String... args) throws Exception {
  3. RestTemplate restTemplate = new RestTemplate();
  4. ResponseWrapper<Quote> quote = restTemplate.exchange("http://gturnquist-quoters.cfapps.io/api/random", HttpMethod.GET, null, new ParameterizedTypeReference<ResponseWrapper<Quote>>() {});
  5. log.info(quote.toString());
  6. }

spring消费RESTfull服务的更多相关文章

  1. 玩转Spring Cloud之服务注册发现(eureka)及负载均衡消费(ribbon、feign)

    如果说用Spring Boot+Spring MVC是开发单体应用(或单体服务)的利器,那么Spring Boot+Spring MVC+Spring Cloud将是开发分布式应用(快速构建微服务)的 ...

  2. Spring Cloud中服务的发现与消费

    之前没注意,微信公众号的图片不能引用到其他地方,本文图片显示不正常,原图在写完博客后已经删了,,,,,,所以本文小伙伴可以移步这里https://mp.weixin.qq.com/s/GoIZdwt5 ...

  3. spring cloud(服务消费者(利用ribbon实现服务消费及负载均衡)——初学二)

    Ribbon是一个基于HTTP和TCP客户端的负载均衡器,利用ribbon实现服务消费,并实现客户端的负载均衡. 一.准备工作(利用上一节的内容) 启动服务注册中心 启动computer-servic ...

  4. 1 Spring Cloud Eureka服务治理

    注:此随笔为读书笔记.<Spring Cloud微服务实战> 什么是微服务? 微服务是将一个原本独立的系统拆分成若干个小型服务(一般按照功能模块拆分),这些小型服务都在各自独立的进程中运行 ...

  5. Dubbo和Spring Cloud微服务架构'

    微服务架构是互联网很热门的话题,是互联网技术发展的必然结果.它提倡将单一应用程序划分成一组小的服务,服务之间互相协调.互相配合,为用户提供最终价值.虽然微服务架构没有公认的技术标准和规范或者草案,但业 ...

  6. Dubbo和Spring Cloud微服务架构比较

    Dubbo 出生于阿里系,是阿里巴巴服务化治理的核心框架,并被广泛应用于中国各互联网公司:只需要通过 Spring 配置的方式即可完成服务化,对于应用无入侵,设计的目的还是服务于自身的业务为主. 微服 ...

  7. 你真的了解微服务架构吗?听听八年阿里架构师怎样讲述Dubbo和Spring Cloud微服务架构

    微服务架构是互联网很热门的话题,是互联网技术发展的必然结果.它提倡将单一应用程序划分成一组小的服务,服务之间互相协调.互相配合,为用户提供最终价值.虽然微服务架构没有公认的技术标准和规范或者草案,但业 ...

  8. 听听八年阿里架构师怎样讲述Dubbo和Spring Cloud微服务架构

    转自:https://baijiahao.baidu.com/s?id=1600174787011483381&wfr=spider&for=pc 微服务架构是互联网很热门的话题,是互 ...

  9. Dubbo 和 Spring Cloud微服务架构 比较及相关差异

    你真的了解微服务架构吗?听听八年阿里架构师怎样讲述Dubbo和Spring Cloud微服务架构. 微服务架构是互联网很热门的话题,是互联网技术发展的必然结果.它提倡将单一应用程序划分成一组小的服务, ...

随机推荐

  1. ProtocalBuffers学习记录

    Google Protocol Buffer 的使用和原理 Google Protocol Buffers 概述 Google Protocol Buffers 入门 Protocol Buffers ...

  2. ThinkPHP 3.2.2 视图模板中使用字符串截取函数

    在项目的 Common/function.php 文件里( 项目结构如图 ) 添加函数: /*字符串截断函数+省略号*/ function subtext($text, $length) { if(m ...

  3. WampServer 在 httpd.conf 中配置多站点 (IP 配置法:不用每次修改 hosts 文件 + 域名配置法 )

    因为要用 ThinkPHP 的当前最新版本 3.2.2,对应要求 PHP 的版本要高于 5.3.0,所以安装了 WampServer 2.2 ( Apache 2.2.21,PHP 5.3.10,My ...

  4. ecshop退款订单原理分析

    ecshop退款订单原理分析 时间:2013-04-12 23:41来源:www.chinab4c.com 作者:ecshop专家 点击:799 咨询qq:760868471咨询旺旺 ecshop退款 ...

  5. Bootstrap页面布局8 - BS常用标签与样式

    常用的Css文字标签 一. <p>文字段落Start,<strong>重要的文字</strong>,<em>倾斜的文字用来提示</em>,& ...

  6. Least_squares 最小二乘法

    https://en.wikipedia.org/wiki/Least_squares 動差估計法( MM, The Method of Moment ) 最小平方法( LSQ, The Method ...

  7. Python中dict的特点、更新dict、遍历dict

    dict的第一个特点是查找速度快,无论dict有10个元素还是10万个元素,查找速度都一样.而list的查找速度随着元素增加而逐渐下降. 不过dict的查找速度快不是没有代价的,dict的缺点是占用内 ...

  8. IOS 入门开发教程

    object-c: http://mobile.51cto.com/iphone-261129.htm Objective-C入门教材 Objective-C入门教材 2011-05-11 15:58 ...

  9. python echo服务器和客户端(客户端可以用telnet之类的)

    发上来记录一下,省得下次再写一遍 服务器:server.py #-*- coding:utf-8 -*- from SocketServer import TCPServer, BaseRequest ...

  10. php保存远程文件到本地的方法

    用到了ob_start();<?php header("Content-type:text/html charset=utf-8"); if(!empty($_POST['p ...