首頁  >  文章  >  springframework.web.client RestClient 是否處理 @requestParam

springframework.web.client RestClient 是否處理 @requestParam

WBOY
WBOY轉載
2024-02-10 17:15:07841瀏覽

php小編香蕉將為大家解答一個關於Spring Framework中的問題:RestClient是否處理@RequestParam?在Spring Framework中,使用RestTemplate進行HTTP請求時,@RequestParam註解用於從請求URL中取得參數。那麼RestClient是否能夠正確處理@RequestParam註解呢?接下來,我們將詳細探討這個問題,幫助您更能理解並使用Spring Framework中的RestClient。

問題內容

我嘗試這樣做:

@getmapping("/db/video/getall")
public responseentity<list<videodto>> getallvideo(@requestparam string owneremail) {        
        return ....;
    }
import org.springframework.web.client.restclient;

restclient restclient = restclient.create("http://localhost:8095");

list<videodto> result = restclient.get()
    .uri("/db/video/getall")
    .accept(mediatype.application_json)
    .retrieve()
    .body(new parameterizedtypereference<list<videodto>>() {});
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot</artifactId>
    <version>3.2.1</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>6.1.2</version>
  </dependency>

但我找不到程式碼範例,顯示 restclient 如何將owneremail 作為@requestparam 傳遞。

我找到了 @pathvariable 程式碼範例。

那麼,restclient 是否處理 @requestparam ?

解決方法

對於 get 請求,您可以在 url 中新增查詢參數。 spring 的 uricomponentsbuilder 簡化了 uri 的操作。

restclient.get()
    .uri(uricomponentsbuilder.frompath("/db/video/getall")
            .queryparam("owneremail", "email").touristring())
    // ...

對於 post 請求,內文可以設定為 multivaluemap。 content-type 可以根據其內容推斷出來。

var parts = new LinkedMultiValueMap<String, Object>();
parts.add("ownerEmail", "emailValue");
restClient.post().uri("/db/video/getall").body(parts).retrieve().body(...);

以上是springframework.web.client RestClient 是否處理 @requestParam的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:stackoverflow.com。如有侵權,請聯絡admin@php.cn刪除