Spring RestTemplate 小结 通过 Spring RestTemplate 调用带请求体的 Delete 方法(Delete With Request Body) 为你详细解读HTTP请求头的具体含意 网页头部的声明应该是用 lang="zh" 还是 lang="zh-cn"? HttpClient使用详解 RestTemplate实践 Spring REST HttpClient使用详解

关于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,设置底层连接,然后增删改查。

请参考RestTemplate实践

唯一需要注意的就是默认的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,则用于提醒服务器哪些是浏览器不接受的内容类型。

参考

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

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

HttpClient使用详解

RestTemplate实践

Spring REST

HttpClient使用详解

另推荐一个好文

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