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

SpringBoot使用RestTemplate實現(xiàn)HTTP請求詳解

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

1. 簡單使用

RestTemplate底層是通過HttpURLConnection實現(xiàn)的。

(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);

第一個參數(shù)表示URL,第二個參數(shù)表示返回類型,第三個參數(shù)表示URI中對應(yīng)的參數(shù),是一個可變長參數(shù),可按順序?qū)懚鄠€參數(shù)。

(2)getForEntity

RestTemplate restTemplate = new RestTemplate();
String url = "http://localhost:8080/users/{userName}/{note}/{start}/{limit}";
//使用map封裝多個參數(shù)
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";
//設(shè)置請求頭
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";
//設(shè)置請求頭
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
//創(chuàng)建請求實體對象
HttpEntity<UserVo> request = new HttpEntity<>(newUserVo, headers);
//請求服務(wù)器
ResponseEntity<User> responseEntity = restTemplate.postForEntity(url, request, User.class);
//獲取響應(yīng)體
User user = responseEntity.getBody();
//獲取響應(yīng)頭
HttpHeaders respHeaders = responseEntity.getHeaders();
//獲取響應(yīng)屬性
List<String> success = respHeaders.get("success");
//獲取響應(yīng)狀態(tài)碼
int statusCode = responseEntity.getStatusCodeValue();

(5)delete

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

(6)exchange

RestTemplate還提供了一個exchange方法,該方法比上面的方法靈活,可以通過制定參數(shù)實現(xiàn)各種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";
//設(shè)置請求頭
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
//創(chuàng)建請求實體對象
HttpEntity<UserVo> request = new HttpEntity<>(newUserVo, headers);
//請求服務(wù)器
ResponseEntity<User> responseEntity = restTemplate.exchange(url,  HttpMethod.POST, request, User.class);
//獲取響應(yīng)體
User user = responseEntity.getBody();
//獲取響應(yīng)頭
HttpHeaders respHeaders = responseEntity.getHeaders();
//獲取響應(yīng)屬性
List<String> success = respHeaders.get("success");
//獲取響應(yīng)狀態(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);
//獲取響應(yīng)體
User user1 = responseEntity1.getBody();

2. 使用泛型

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

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

if (responseEntity != null) {
    //獲取響應(yīng)體
    String body = responseEntity.getBody();
    //使用Jackson轉(zhuǎn)為對應(yīng)的實體類,這里的Result中使用到了泛型,用來應(yīng)對多種格式
    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實現(xiàn)HTTP請求詳解的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot RestTemplate實現(xiàn)HTTP請求的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

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

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

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

    SpringBoot使用CXF集成WebService的方法

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

    SpringBoot詳解整合Redis緩存方法

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

    Java返回分頁結(jié)果集的封裝代碼實例

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

    關(guān)于Java中的可見性和有序性問題

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

    java用LocalDateTime類獲取當(dāng)天時間、前一天時間及本周/本月的開始和結(jié)束時間

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

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

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

    代碼分析Spring MVC的工作原理

    在本篇文章里小編給大家整理了關(guān)于Spring MVC的工作原理的相關(guān)知識點以及實例代碼內(nèi)容,需要的朋友們可以參考下。
    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管理的對象都可以實現(xiàn)該接口,需要的朋友可以參考下
    2023-05-05

最新評論

方山县| 洪泽县| 万盛区| 自贡市| 甘德县| 万盛区| 浦东新区| 西华县| 巴彦淖尔市| 古蔺县| 定南县| 米易县| 大同县| 昌平区| 天气| 金湖县| 九江县| 岑溪市| 吕梁市| 陆丰市| 砀山县| 崇信县| 凤山县| 那坡县| 贵南县| 锡林郭勒盟| 抚松县| 民和| 平江县| 肥乡县| 长岛县| 安新县| 黑龙江省| 莱阳市| 中方县| 昭苏县| 青海省| 济南市| 林芝县| 龙井市| 象州县|