作为springBoot的开篇系列,RestTemplate只能表示我只是个意外

what

RestTemplate是spring提供的用于访问rest服务的客户端(其实类似Apache的HttpClient,封装度更高一点)。默认是基于java.net包实现的,没有连接池的概念,也可以设置Apache的HttpClient作为作为实现。和RestTemplate功能相似的有Feign,不过Feign个人感觉有点为了封装而封装,有点多余。

Why

支持的常用的http方法

方法

作用

RestTemplate

GET

请求指定的页面信息,并返回实体主体

getForObject(String, Class, String...)

HEAD

只请求页面的首部

headForHeaders(String, String...)

POST

请求服务器接受所指定的文档作为对所标识的URI的新的从属实体

postForLocation(String, Object, String...)

PUT

从客户端向服务器传送的数据取代指定的文档的内容

put(String, Object, String...)

DELETE

请求服务器删除指定的页面

delete(String, String...)

OPTIONS

允许客户端查看服务器的性能

optionsForAllow(String, String...)

特性

线程安全

自定义回调操作

支持领域实体,可以直接将json转成javabean

支持URI模板

How

Maven配置

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>org.springframework</groupId>

    <artifactId>gs-consuming-rest</artifactId>

    <version>0.1.0</version>

    <parent>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-parent</artifactId>

        <version>1.5.2.RELEASE</version>

    </parent>

    <properties>

        <java.version>1.8</java.version>

    </properties>

    <dependencies>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter</artifactId>

        </dependency>

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-web</artifactId>

        </dependency>

        <dependency>

            <groupId>com.fasterxml.jackson.core</groupId>

            <artifactId>jackson-databind</artifactId>

        </dependency>

    </dependencies>

    <build>

        <plugins>

            <plugin>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-maven-plugin</artifactId>

            </plugin>

        </plugins>

    </build>

</project>

测试代码

 

普通java调用风格

RestTemplate restTemplate = new RestTemplate();

SimpleClientHttpRequestFactory requestFactory = (SimpleClientHttpRequestFactory) restTemplate.getRequestFactory();
//常用超时设置 requestFactory.setReadTimeout(6000); requestFactory.setConnectTimeout(6000); restTemplate.setRequestFactory(requestFactory); try{ String res = restTemplate.getForObject("http://gturnquist-quoters.cfapps.io/api/random",String.class); System.out.println("res ="+ res); }catch (RestClientException e){ e.printStackTrace(); }

Spring调用风格

生成RestTemplate实例,自动添加到spring容器中

@Configuration

public class RestTemplateTest {

    @Bean

    public RestTemplate getRestTemplate(){

        RestTemplate restTemplate = new RestTemplate();

        SimpleClientHttpRequestFactory requestFactory = (SimpleClientHttpRequestFactory) restTemplate.getRequestFactory();
//常用超时设置 requestFactory.setReadTimeout(6000); requestFactory.setConnectTimeout(6000); restTemplate.setRequestFactory(requestFactory); return restTemplate; } } //调用的地方自动装载RestTemplate实例 @Autowired RestTemplate restTemplate;
try{
String res = restTemplate.getForObject("http://gturnquist-quoters.cfapps.io/api/random",String.class);
System.out.println("res ="+ res);
}catch (RestClientException e){
e.printStackTrace();
}

uri模板

可以只替换参数,不改变uri结构

String result = restTemplate.getForObject("http://example.com/hotels/{hotel}/bookings/{booking}", String.class, "42", "21");
Map<String, String> vars = new HashMap<String, String>();
vars.put("hotel", "42");
vars.put("booking", "21");
String result = restTemplate.getForObject("http://example.com/hotels/{hotel}/bookings/{booking}", String.class, vars);

参考资料

//Spring官网对RestTemplate的介绍

https://spring.io/blog/2009/03/27/rest-in-spring-3-resttemplate/

//spring官网对RestTemplate的使用向导

https://spring.io/guides/gs/consuming-rest/

//应该是目前RestTemplate介绍比较多的博客

http://liuxing.info/2015/05/21/RestTemplate%E5%AE%9E%E8%B7%B5/

SpringBoot系列(一)RestTemplate的更多相关文章

  1. SpringBoot系列: RestTemplate 快速入门

    ====================================相关的文章====================================SpringBoot系列: 与Spring R ...

  2. SpringBoot系列之集成logback实现日志打印(篇二)

    SpringBoot系列之集成logback实现日志打印(篇二) 基于上篇博客SpringBoot系列之集成logback实现日志打印(篇一)之后,再写一篇博客进行补充 logback是一款开源的日志 ...

  3. springBoot系列教程07:异常捕获

    发生异常是很正常的事,异常种类也是千奇百怪,发生异常并不可怕,只要正确的处理,并正确的返回错误信息并无大碍,如果不进行捕获或者处理,分分钟服务器宕机是很正常的事 所以处理异常时,最基本的要求就是发生异 ...

  4. SpringBoot系列——利用系统环境变量与配置文件的分支选择实现“智能部署”

    前言 通过之前的博客:SpringBoot系列——jar包与war包的部署,我们已经知道了如果实现项目的简单部署,但项目部署的时候最烦的是什么?修改成发布环境对应的配置!数据库连接地址.Eureka注 ...

  5. Springboot 系列(十二)使用 Mybatis 集成 pagehelper 分页插件和 mapper 插件

    前言 在 Springboot 系列文章第十一篇里(使用 Mybatis(自动生成插件) 访问数据库),实验了 Springboot 结合 Mybatis 以及 Mybatis-generator 生 ...

  6. Springboot 系列(九)使用 Spring JDBC 和 Druid 数据源监控

    前言 作为一名 Java 开发者,相信对 JDBC(Java Data Base Connectivity)是不会陌生的,JDBC作为 Java 基础内容,它提供了一种基准,据此可以构建更高级的工具和 ...

  7. SpringBoot系列——Spring-Data-JPA(究极进化版) 自动生成单表基础增、删、改、查接口

    前言 我们在之前的实现了springboot与data-jpa的增.删.改.查简单使用(请戳:SpringBoot系列——Spring-Data-JPA),并实现了升级版(请戳:SpringBoot系 ...

  8. SpringBoot系列——Spring-Data-JPA

    前言 jpa是ORM映射框架,更多详情,请戳:apring-data-jpa官网:http://spring.io/projects/spring-data-jpa,以及一篇优秀的博客:https:/ ...

  9. SpringBoot系列——Spring-Data-JPA(升级版)

    前言 在上篇博客中:SpringBoot系列——Spring-Data-JPA:https://www.cnblogs.com/huanzi-qch/p/9970545.html,我们实现了单表的基础 ...

随机推荐

  1. D3.js-数值自动变动的条形图表

    开始停止   // <p> <style><!-- button{ background-color:#aaaaaa; font-family:微软雅黑; font-si ...

  2. State模式学习笔记

    选用了一个假设需要用户验证的例子进行State模式学习,这个例子并不恰当.无所谓了,只要能学习到其中的内容即可. 适用性: 1,一个对象的行为取决于他的状态,并且它必须在运行时刻依据状态改变他的行为. ...

  3. Centos7 tmux1.6 安装

    环境:Centos 7.0 安装tmux之前需要先安装一些支持的组件: yum install libevent-devel ncurses-devel 接下来就是下载源码包进行安装了,这里要说明一点 ...

  4. C++中的类继承(1) 三种继承方式

    继承是使代码可以复用的重要手段,也是面向对象程序设计的核心思想之一.简单的说,继承是指一个对象直接使用另一对象的属性和方法.继承呈现了 面向对象程序设 计的层次结构, 体现了 由简单到复杂的认知过程. ...

  5. 搭建MySQL高可用负载均衡集群

    1.简介 使用MySQL时随着时间的增长,用户量以及数据量的逐渐增加,访问量更是剧增,最终将会使MySQL达到某个瓶颈,那么MySQL的性能将会大大降低.这一结果也不利于软件的推广. 那么如何跨过这个 ...

  6. 如何在 FineUIMvc 中引用第三方 JavaScript 库

    声明:FineUIMvc(基础版)是免费软件,本系列文章适用于基础版. 引入第三方颜色选择器 在 FineUIMvc 中使用第三方 JavaScript 遵循一定的约定,也非常简单. 下面以官网示例为 ...

  7. Broker节点

    在druid集群环境中 broker节点的作用是查询.它知道metadata 通过zookeeper发送到了集群中的哪个节点,从而能够准确的查询到.broker也把各个节点的结果汇聚到一个节点中.On ...

  8. NOIP2015游记——一次开心又失望的旅行

    啊,一年一度的NOIP终于是结束了 以前的大神都有写自己的感受 然而我居然给忘了!!!! 吓得我赶紧来写一份游记 Day.-INF--出发前一个星期 机智的我选择了停课 就是为了OIER这伟大而又光荣 ...

  9. struts2 之 struts2类型转换

    1. 在struts2中,相比servlet来时,获取数据时,程序员没有进行手动的类型转换,类型转换工作都有struts2来完成处理,但愿对于自定义类型数据,struts2不会帮助我们完成类型转换工作 ...

  10. JS对象或属性的不变性

    提到不变性,不得不提一个概念: 对象常量定义:结合可写性与可配置性可以创建一个真正的常量属性(不可修改.重定义.删除) 不变性可划分为以下几个等级: 1)禁止扩展:Object.preventExte ...