最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot使用RestTemplate實現HTTP請求詳解

 更新時間:2024年03月21日 08:21:49   作者:一線大碼  
這篇文章主要為大家詳細介紹了SpringBoot如何使用RestTemplate實現進行HTTP請求,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下

1. 簡單使用

RestTemplate底層是通過HttpURLConnection實現的。

(1)getForObject

RestTemplate restTemplate = new RestTemplate(https://blog.csdn.net/mryang125/article/details/80955558);
String url = "http://localhost:8080/user/{id}";
UserVo userVo = restTemplate.getForObject(url, UserVo.class, id);

第一個參數表示URL,第二個參數表示返回類型,第三個參數表示URI中對應的參數,是一個可變長參數,可按順序寫多個參數。

(2)getForEntity

RestTemplate restTemplate = new RestTemplate();
String url = "http://localhost:8080/users/{userName}/{note}/{start}/{limit}";
//使用map封裝多個參數
Map<String, Object> params = new HashMap<>();
params.put("userName", userName);
params.put("note", note);
params.put("start", start);
params.put("limit", limit);
ResponseEntity<List> responseEntity = restTemplate.getForEntity(url, List.class, params);
List<UserVo> userVos = responseEntity.getBody();

(3)postForObject

RestTemplate restTemplate = new RestTemplate();
String url = "http://localhost:8080/user";
//設置請求頭
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
//創(chuàng)建請求實體對象
HttpEntity<UserVo> request = new HttpEntity<>(newUserVo, headers);
User user = restTemplate.postForObject(url, request, User.class);

(4)postForEntity

RestTemplate restTemplate = new RestTemplate();
String url = "http://localhost:8080/user";
//設置請求頭
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
//創(chuàng)建請求實體對象
HttpEntity<UserVo> request = new HttpEntity<>(newUserVo, headers);
//請求服務器
ResponseEntity<User> responseEntity = restTemplate.postForEntity(url, request, User.class);
//獲取響應體
User user = responseEntity.getBody();
//獲取響應頭
HttpHeaders respHeaders = responseEntity.getHeaders();
//獲取響應屬性
List<String> success = respHeaders.get("success");
//獲取響應狀態(tài)碼
int statusCode = responseEntity.getStatusCodeValue();

(5)delete

RestTemplate restTemplate = new RestTemplate();
restTemplate.delete("http://localhost:8080/use/{id}", id);

(6)exchange

RestTemplate還提供了一個exchange方法,該方法比上面的方法靈活,可以通過制定參數實現各種Http請求。下面列出Spring提供的八種方法。

<T> ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, Class<T> responseType, Object... uriVariables) throws RestClientException;

<T> ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, Class<T> responseType, Map<String, ?> uriVariables) throws RestClientException;

<T> ResponseEntity<T> exchange(URI url, HttpMethod method, HttpEntity<?> requestEntity, Class<T> responseType) throws RestClientException;

<T> ResponseEntity<T> exchange(String url,HttpMethod method, HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseType, Object... uriVariables) throws RestClientException;

<T> ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseType, Map<String, ?> uriVariables) throws RestClientException;

<T> ResponseEntity<T> exchange(URI url, HttpMethod method, HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseType) throws RestClientException;

<T> ResponseEntity<T> exchange(RequestEntity<?> requestEntity, Class<T> responseType) throws RestClientException;

<T> ResponseEntity<T> exchange(RequestEntity<?> requestEntity, ParameterizedTypeReference<T> responseType) throws RestClientException;

下面寫一個使用例子:

RestTemplate restTemplate = new RestTemplate();
String url = "http://localhost:8080/user";
//設置請求頭
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
//創(chuàng)建請求實體對象
HttpEntity<UserVo> request = new HttpEntity<>(newUserVo, headers);
//請求服務器
ResponseEntity<User> responseEntity = restTemplate.exchange(url,  HttpMethod.POST, request, User.class);
//獲取響應體
User user = responseEntity.getBody();
//獲取響應頭
HttpHeaders respHeaders = responseEntity.getHeaders();
//獲取響應屬性
List<String> success = respHeaders.get("success");
//獲取響應狀態(tài)碼
int statusCode = responseEntity.getStatusCodeValue();

//獲取資源
String url1 = "http://localhost:8080/user/{id}";
ResponseEntity<User> responseEntity1 = restTemplate.exchange(url1,  HttpMethod.GET, null, User.class, id);
//獲取響應體
User user1 = responseEntity1.getBody();

2. 使用泛型

使用泛型接收響應,這里添加一個返回類型中泛型的使用方法:

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

//創(chuàng)建請求實體對象,這里將參數轉換為JSON字符串了
HttpEntity<String> request = new HttpEntity<>(paramJsonStr, headers);
//請求服務器
RestTemplate restTemplate = new RestTemplate();
String url = "http://www.baidu.com/task";
ResponseEntity<String> responseEntity = null;
try {
	//統(tǒng)一使用String接收響應,再用Jackson轉為對應的實體類
    responseEntity = restTemplate.postForEntity(url, request, String.class);
//這里使用try...catch是因為有可能因為網絡原因出現錯誤,RestClientException 的子類 ResourceAccessException 異常
} catch (RestClientException e) {
    e.printStackTrace();
}

if (responseEntity != null) {
    //獲取響應體
    String body = responseEntity.getBody();
    //使用Jackson轉為對應的實體類,這里的Result中使用到了泛型,用來應對多種格式
    ObjectMapper mapper = new ObjectMapper();
    Result<Ret> result = mapper.readValue(body, new TypeReference<Result<Ret>>() {});
    if (result != null) {
    	//使用了泛型,不同的請求這里就可以獲取到不同類型的data
        Ret data = result.getData();
        System.out.println("data : " + data);
    }
}
@Data
public class Result<T> {
    private String logid;
    private T data;
    private Integer status;
    private String message;
    private Double st;
    private Double crt;
}
@Data
public class Ret {
    private Boolean ret;
    private String photoId;
}

以上就是SpringBoot使用RestTemplate實現HTTP請求詳解的詳細內容,更多關于SpringBoot RestTemplate實現HTTP請求的資料請關注腳本之家其它相關文章!

相關文章

  • Spring Boot 全局異常處理策略設計之@ExceptionHandler 與 @ControllerAdvice 生效原理源碼解析

    Spring Boot 全局異常處理策略設計之@ExceptionHandler 與&nb

    SpringBoot全局異常處理策略設計@ExceptionHandler與@ControllerAdvice生效原理源碼解析,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2026-01-01
  • SpringBoot使用CXF集成WebService的方法

    SpringBoot使用CXF集成WebService的方法

    這篇文章主要介紹了SpringBoot使用CXF集成WebService的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-08-08
  • SpringBoot詳解整合Redis緩存方法

    SpringBoot詳解整合Redis緩存方法

    本文主要介紹了SpringBoot整合Redis緩存的實現方法,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • Java返回分頁結果集的封裝代碼實例

    Java返回分頁結果集的封裝代碼實例

    這篇文章主要介紹了java返回分頁結果集的封裝代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-01-01
  • 關于Java中的可見性和有序性問題

    關于Java中的可見性和有序性問題

    這篇文章主要介紹了關于Java中的可見性和有序性問題,Java在誕生之初就支持多線程,自然也有針對這三者的技術方案,今天就學習一下Java如何解決其中的可見性和有序性導致的問題,需要的朋友可以參考下
    2023-08-08
  • java用LocalDateTime類獲取當天時間、前一天時間及本周/本月的開始和結束時間

    java用LocalDateTime類獲取當天時間、前一天時間及本周/本月的開始和結束時間

    這篇文章主要給大家介紹了關于java使用LocalDateTime類獲取當天時間、前一天時間及本周/本月的開始和結束時間的相關資料,文中通過代碼示例介紹的非常詳細,需要的朋友可以參考下
    2023-08-08
  • Java冒泡排序的定義與實例代碼

    Java冒泡排序的定義與實例代碼

    這篇文章主要給大家介紹了關于Java冒泡排序的定義與實例的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-12-12
  • 代碼分析Spring MVC的工作原理

    代碼分析Spring MVC的工作原理

    在本篇文章里小編給大家整理了關于Spring MVC的工作原理的相關知識點以及實例代碼內容,需要的朋友們可以參考下。
    2019-06-06
  • Maven發(fā)布項目 (jar包) 到Nexus私服中的操作

    Maven發(fā)布項目 (jar包) 到Nexus私服中的操作

    這篇文章主要介紹了Maven發(fā)布項目 (jar包) 到Nexus私服中的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10
  • Spring中的Lifecycle接口使用與源碼分析

    Spring中的Lifecycle接口使用與源碼分析

    這篇文章主要介紹了Spring中的Lifecycle接口使用與源碼分析,LifeCycle接口定義了Spring容器的生命周期,任何被Spring管理的對象都可以實現該接口,需要的朋友可以參考下
    2023-05-05

最新評論

阳泉市| 巨野县| 措美县| 甘孜县| 九龙城区| 大竹县| 武宣县| 临高县| 石首市| 灵石县| 灵石县| 湖北省| 肇州县| 定安县| 玉屏| 龙游县| 巴林左旗| 湘潭市| 卓尼县| 乐昌市| 宜州市| 措勤县| 上犹县| 肥西县| 静安区| 沅江市| 松滋市| 德庆县| 建瓯市| 长丰县| 张家口市| 普兰店市| 绩溪县| 曲沃县| 峨眉山市| 体育| 西青区| 龙州县| 延庆县| 巢湖市| 清河县|