簡(jiǎn)化API提升開發(fā)效率RestTemplate與HttpClient?OkHttp關(guān)系詳解
1. 什么是RestTemplate
RestTemplate是Spring提供的用于訪問Rest服務(wù)的客戶端。
2. RestTemplate與HttpClient、OkHttp等的關(guān)系
RestTemplate是在其他HTTP客戶端庫(kù)基礎(chǔ)上的再次封裝。相對(duì)于其他庫(kù),RestTemplate提供了更加簡(jiǎn)單易用的API,降低了上手和使用成本,提升開發(fā)效率。
3. 配置RestTemplate
3.1. 引入依賴
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>RestTemplate默認(rèn)使用JDK的HttpURLConnection作為底層HTTP客戶端的實(shí)現(xiàn)。
如果要使用其他HTTP客戶端庫(kù),請(qǐng)自行引入依賴。
3.2. 初始化為Bean
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
return new RestTemplate(factory);
}
@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setReadTimeout(5000);
factory.setConnectTimeout(10000);
return factory;
}
}4. 常用API
4.1 Get請(qǐng)求
//該方法僅返回HTTP協(xié)議的響應(yīng)體,如果你只關(guān)注返回的內(nèi)容,用這個(gè)方法即可 public <T> T getForObject(String url, Class<T> responseType, Object... uriVariables ); //該方法返回ResponseEntity,包含了整個(gè)HTTP響應(yīng) public <T> org.springframework.http.ResponseEntity<T> getForEntity(String url, Class<T> responseType, Object... uriVariables ); public <T> org.springframework.http.ResponseEntity<T> getForEntity(String url, Class<T> responseType, java.util.Map<String, ?> uriVariables );
4.2 Post請(qǐng)求
Post請(qǐng)求的API與Get請(qǐng)求的API相對(duì)應(yīng),功能上相類似。
//該方法僅返回HTTP協(xié)議的響應(yīng)體,如果你只關(guān)注返回的內(nèi)容,用這個(gè)方法即可 public <T> T postForObject(String url, @Nullable Object request, Class<T> responseType, Object... uriVariables ); //該方法返回ResponseEntity,包含了整個(gè)HTTP響應(yīng) public <T> T postForObject(String url, @Nullable Object request, Class<T> responseType, Object... uriVariables ); public <T> T postForObject(String url, @Nullable Object request, Class<T> responseType, java.util.Map<String, ?> uriVariables );
4.3 exchange方法
//通用API public <T> org.springframework.http.ResponseEntity<T> exchange(String url, org.springframework.http.HttpMethod method, @Nullable org.springframework.http.HttpEntity<?> requestEntity, Class<T> responseType, Object... uriVariables ); public <T> org.springframework.http.ResponseEntity<T> exchange(String url, org.springframework.http.HttpMethod method, @Nullable org.springframework.http.HttpEntity<?> requestEntity, Class<T> responseType, java.util.Map<String, ?> uriVariables );
4.4 一些示例
String res = restTemplate.getForObject(sb.toString(), String.class); Map result = JSON.parseObject(res, Map.class); String res = restTemplate.postForObject(url, requestBody, String.class); Map result = JSON.parseObject(res, Map.class); //可以用來下載圖片 HttpEntity httpEntity = new HttpEntity<>(requestBody); ResponseEntity<byte[]> responseEntity = restTemplate.exchange(url, HttpMethod.POST, httpEntity, byte[].class); byte[] buffer = responseEntity.getBody();
以上就是RestTemplate與HttpClient OkHttp關(guān)系簡(jiǎn)化API提升開發(fā)效率的詳細(xì)內(nèi)容,更多關(guān)于RestTemplate HttpClient OkHttp的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
java簡(jiǎn)單實(shí)現(xiàn)用語(yǔ)音讀txt文檔方法總結(jié)
在本篇文章里小編給大家整理了關(guān)于java簡(jiǎn)單實(shí)現(xiàn)用語(yǔ)音讀txt文檔的詳細(xì)方法總結(jié),有需要的朋友們參考下。2019-06-06
swing中Tree與滾動(dòng)條用法實(shí)例分析
這篇文章主要介紹了swing中Tree與滾動(dòng)條用法,以實(shí)例形式分析了java基于swing實(shí)現(xiàn)圖形界面的使用技巧,需要的朋友可以參考下2015-09-09
Java實(shí)戰(zhàn)之客戶信息管理系統(tǒng)
這篇文章主要介紹了Java實(shí)戰(zhàn)之客戶信息管理系統(tǒng),文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04
SpringBoot 如何實(shí)現(xiàn)Session共享
這篇文章主要介紹了SpringBoot 如何實(shí)現(xiàn)Session共享,幫助大家更好的理解和學(xué)習(xí)spring boot框架,感興趣的朋友可以了解下2020-09-09
java 四舍五入保留小數(shù)的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄猨ava 四舍五入保留小數(shù)的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-09-09

