RestTemplate中幾種常見的請求方式

2022-06-29 20:27:13 字數 834 閱讀 7026

getforentity方法的返回值是乙個responseentityresponseentity是spring對http請求響應的封裝,包括了幾個重要的元素,如響應碼、contenttype、contentlength、響應訊息體等。

可以用乙個數字做佔位符,最後是乙個可變長度的引數,來一一替換前面的佔位符

public

string sayhello() ", string.class, "張三");

return

responseentity.getbody();

}可以前面使用name=這種形式,最後乙個引數是乙個map,map的key即為前邊佔位符的名字,map的value為引數值

public

string sayhello2() ", string.class

, map);

return

responseentity.getbody();

}getforobject函式實際上是對getforentity函式的進一步封裝,如果你只關注返回的訊息體的內容,對其他資訊都不關注,此時可以使用getforobject

public

book book2()

方法的第一引數表示要呼叫的服務的位址 第二個引數表示上傳的引數 第三個引數表示返回的訊息體的資料型別

public

book book3()

服務端public

book book2(@requestbody book book)

如果你只關注,返回的訊息體,可以直接使用postforobject。用法和getforobject一致。

RestTemplate中幾種常見請求方法的使用

在resttemplate中,傳送乙個get請求,我們可以通過如下兩種方式 getforentity方法的返回值是乙個responseentity,responseentity是spring對http請求響應的封裝,包括了幾個重要的元素,如響應碼 contenttype contentlength ...

關於RestTemplate幾種常見的方式

spring中使用rest資源,借助這個可以輕鬆訪問資源,大多是與http互動的方法 getforobject 傳送乙個http get請求,返回的請求體將對映為乙個物件 postforentity post 資料到乙個url,返回包含乙個物件的responseentity,這個物件是從響應體中對映...

Spring中RestTemplate的使用

1 帶引數的get請求 請求url示例 http localhost 8080 test sendsms?phone 手機號 msg 簡訊內容 錯誤使用 autowired private restoperations restoperations public void test throws e...