关于RestTemplate

首先,你可以把它理解为一个发起请求并接收响应的工具类(功能类似浏览器)。

其次,它其实是一个壳,具体还是通过调用别的接口来实现(如jdk自带的连接,或者HttpClient之类的,需要设置)。

官方介绍

Spring's central class for synchronous client-side HTTP access. 
It simplifies communication with HTTP servers, and enforces RESTful principles.
It handles HTTP connections, leaving application code to provide URLs (with possible template variables) and extract results. Note: by default the RestTemplate relies on standard JDK facilities to establish HTTP connections.
You can switch to use a different HTTP library such as Apache HttpComponents, Netty, and OkHttp
through the HttpAccessor.setRequestFactory(org.springframework.http.client.ClientHttpRequestFactory) property.

使用

这个其实没什么说的,不外乎创建template,设置底层连接,然后增删改查。

请参考Spring RESTRestTemplate实践

唯一需要注意的就是默认的jdk连接是不支持delete带请求体--解决办法见下面链接。

其他

暂时不清楚使用RestTemplate和直接使用HttpClient有什么区别,感觉HttpClient足够用了--囧。

--突然想到的,RestTemplate可以配合MessageConverter等使用!!!也就是配合SpringMVC使用,而HttpClient则需要手动转换!

杂:

httpHeaders.add("Accept-Language", "zh-CN,zh;q=0.8,en;q=0.6");

q是权重系数,范围 0 =< q <= 1,q 值越大,请求越倾向于获得其“;”之前的类型表示的内容,若没有指定 q 值,则默认为1,若被赋值为0,则用于提醒服务器哪些是浏览器不接受的内容类型。

参考:

通过 Spring RestTemplate 调用带请求体的 Delete 方法(Delete With Request Body)

为你详细解读HTTP请求头的具体含意

网页头部的声明应该是用 lang="zh" 还是 lang="zh-cn"?

HttpClient使用详解

RestTemplate实践

Spring REST

HttpClient使用详解

另推荐一个好文

SpringMVC源码剖析(五)-消息转换器HttpMessageConverter

Spring RestTemplate 小结的更多相关文章

  1. Spring RestTemplate: 比httpClient更优雅的Restful URL访问, java HttpPost with header

    { "Author": "tomcat and jerry", "url":"http://www.cnblogs.com/tom ...

  2. Spring RestTemplate介绍

    http://www.cnblogs.com/rollenholt/p/3894117.html RestTemplate 这篇文章打算介绍一下Spring的RestTemplate.我这边以前设计到 ...

  3. How to Send an HTTP Header With Every Request With Spring RestTemplate

    In Know Which Apps Are Hitting Your Web Service, I showed how to write a servlet filter that enforce ...

  4. Spring RestTemplate详解

    Spring RestTemplate详解   1.什么是REST? REST(RepresentationalState Transfer)是Roy Fielding 提出的一个描述互联系统架构风格 ...

  5. Spring RestTemplate中几种常见的请求方式GET请求 POST请求 PUT请求 DELETE请求

    Spring RestTemplate中几种常见的请求方式 原文地址: https://blog.csdn.net/u012702547/article/details/77917939   版权声明 ...

  6. How to disable SSL certificate checking with Spring RestTemplate?(使用resttemplate访问https时禁用证书检查)

    How to disable SSL certificate checking with Spring RestTemplate?(使用resttemplate访问https时禁用证书检查) **** ...

  7. spring RestTemplate用法详解

    spring RestTemplate用法详解 spring 3.2.3 框架参考有说明 21.9 Accessing RESTful services on the Client

  8. 使用HttpClient4来构建Spring RestTemplate

    Spring RestTemplate简单说明 现在REST服务已经很普及了,在我们的程序中,经常会需要调用REST API,这时候会有很多选择,原始一点的JDK自带的,再进一步点使用HttpClie ...

  9. spring restTemplate 用法

    发出get请求,方式一 String url = serverUrl+"/path/path?id={id}"; int i = restTemplate.getForObject ...

随机推荐

  1. 删除vs中最近的项目的方法

    Microsoft Visual Studio中可以自行设置显示多少个最近打开的项目,但有些时候会建个项目做测试,用完了就删了,却总显示在“文件”-“最近的项目”菜单中以及“起始页”-“打开现有项目” ...

  2. eclipse逆向生成实体类注解方式或者xml方式

    转载自:http://www.2cto.com/database/201501/372023.html http://blog.csdn.net/wangpeng047/article/details ...

  3. 【转】使用create_project.py创建cocos2d项目时出错

    命令格式:create_project.py -project 项目名字 -package 包名 -language cpp,例如: create_project.py -project HelloC ...

  4. [转]WCF类型共享技巧

    调用过WCF服务的同学可能都会遇到这样的问题,同一个实体类型,不同的服务Visual Studio生成了不同的版本,例如Service1.User和Service2.User,对于C#来说,这是两个不 ...

  5. Mac 下的矢量图设计工具

    Mac 下的矢量图设计工具 一图胜千言.一张清晰的示意图无论对于系统设计,流程梳理,还是其他的方方面面,都非常重要. 曾经亲见一位老同事把 FreeHand 这个矢量绘图工具用得出神入化,并且非常成功 ...

  6. 【Android】20.4 录音

    分类:C#.Android.VS2015: 创建日期:2016-03-13 一.简介 利用Android提供的MediaRecorder类可直接录制音频. 1.权限要求 录制音频和视频需要下面的权限: ...

  7. 【Android】6.2 AlertDialog(警告对话框)

    分类:C#.Android.VS2015: 创建日期:2016-02-08 一.简介 AlertDialog也是Android系统当中常用的对话框之一. 在一个AlertDialog中,可以有一个Bu ...

  8. HAProxy负载均衡原理及企业级实例部署haproxy集群

    一 HAProxy简介   HAProxy是一种高效.可靠.免费的高可用及负载均衡解决方案,非常适合于高负载站点的七层数据请求.客户端通过HAProxy代理服务器获得站点页面,而代理服务器收到客户请求 ...

  9. mysqldump 报导常

    Warning: A partial dump from a server that has GTIDs will by default include the GTIDs of all transa ...

  10. [转帖]cocos2D-X源码分析之从cocos2D-X学习OpenGL(3)----BATCH_COMMAND

    原贴: cocos2D-X源码分析之从cocos2D-X学习OpenGL(3)----BATCH_COMMAND 上一篇介绍了QUAD_COMMAND渲染命令,顺带介绍了VAO和VBO,这一篇介绍批处 ...