簡體   English   中英

Spring Framework WebFlux 響應式編程

[英]Spring Framework WebFlux Reactive Programming

我正在嘗試向端點發送一個對象,但我不明白為什么我不能用 .get() 來做到這一點,為什么必須使用 .post()? 如果端點方法接受一個對象並用它做一些事情並返回一個對象怎么辦? 我可能想向端點發送一個對象,該端點將對象作為參數。 有沒有辦法做到這一點? 如何將客戶對象傳遞給 getCustomer() 端點。

WebClient.create("http://localhost:8080")
            .get()//why this can not be used? why post has to be used?
            .uri("client/getCustomer")
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue(customer)//with .get() body cannot be passed.
            .retrieve()
            .bodyToMono(Customer.class);


        @GET
        @Path("/getCustomer")
        @Produces(MediaType.APPLICATION_JSON)
        @Consumes(MediaType.APPLICATION_JSON)
        public Customer getCustomer(Customer customer) {
            //do something
            return customer;
        }

已編輯

GET方法中,數據在 URL 中發送。 就像: http : //www.test.com/users/1

POST方法中,數據存儲在 HTTP 請求的請求體中。

因此,我們不應期望 .get() 方法具有 .bodyValue()。

現在如果你想使用 GET 方法發送數據,你應該在 URL 中發送它們,如下面的代碼片段

   WebClient.create("http://localhost:8080")
            .get()
            .uri("client/getCustomer/{customerName}" , "testName")
            .retrieve()
            .bodyToMono(Customer.class);

有用的 Spring webClient 示例:

spring 5 WebClient 和 WebTestClient 教程與示例

有關 POST 和 GET 的更多信息

HTTP 請求方法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM