簡化API提升開發(fā)效率RestTemplate與HttpClient?OkHttp關(guān)系詳解
1. 什么是RestTemplate
RestTemplate是Spring提供的用于訪問Rest服務(wù)的客戶端。
2. RestTemplate與HttpClient、OkHttp等的關(guān)系
RestTemplate是在其他HTTP客戶端庫基礎(chǔ)上的再次封裝。相對于其他庫,RestTemplate提供了更加簡單易用的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客戶端庫,請自行引入依賴。
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請求
//該方法僅返回HTTP協(xié)議的響應(yīng)體,如果你只關(guān)注返回的內(nèi)容,用這個方法即可 public <T> T getForObject(String url, Class<T> responseType, Object... uriVariables ); //該方法返回ResponseEntity,包含了整個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請求
Post請求的API與Get請求的API相對應(yīng),功能上相類似。
//該方法僅返回HTTP協(xié)議的響應(yīng)體,如果你只關(guān)注返回的內(nèi)容,用這個方法即可 public <T> T postForObject(String url, @Nullable Object request, Class<T> responseType, Object... uriVariables ); //該方法返回ResponseEntity,包含了整個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)系簡化API提升開發(fā)效率的詳細(xì)內(nèi)容,更多關(guān)于RestTemplate HttpClient OkHttp的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
親手教你IDEA2020.3創(chuàng)建Javaweb項(xiàng)目的步驟詳解
這篇文章主要介紹了IDEA2020.3創(chuàng)建Javaweb項(xiàng)目的步驟詳解,本文是小編手把手教你,通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2021-03-03SpringBoot整合logback日志的詳細(xì)步驟
這篇文章主要介紹了SpringBoot整合logback日志的詳細(xì)步驟,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-05-05SpringCloud中的分布式鎖用法示例詳解(Java+Redis SETNX命令)
在Spring Cloud項(xiàng)目中,使用Java和Redis結(jié)合實(shí)現(xiàn)的分布式鎖可以確保訂單的一致性和并發(fā)控制,分布式鎖的使用能夠在多個實(shí)例同時提交訂單時,僅有一個實(shí)例可以成功進(jìn)行操作,本文給大家介紹Spring,Cloud中的分布式鎖用法詳解(Java+Redis SETNX命令),感興趣的朋友一起看看吧2023-10-10Spring Cloud Gateway網(wǎng)關(guān)XSS過濾方式
這篇文章主要介紹了Spring Cloud Gateway網(wǎng)關(guān)XSS過濾方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10使用springboot在工具類中讀取配置文件(ClassPathResource)
這篇文章主要介紹了使用springboot在工具類中讀取配置文件(ClassPathResource),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08