简体   繁体   English

Spring Framework WebFlux 响应式编程

[英]Spring Framework WebFlux Reactive Programming

I am trying to send an object to the endpoint but I do not understand why I can't do it with .get(), why .post() has to be used?我正在尝试向端点发送一个对象,但我不明白为什么我不能用 .get() 来做到这一点,为什么必须使用 .post()? What if the endpoint method takes an object and does something with it and returns an object?如果端点方法接受一个对象并用它做一些事情并返回一个对象怎么办? I may want to send an object to the endpoint which takes the object as an argument.我可能想向端点发送一个对象,该端点将对象作为参数。 Is there a way to do it?有没有办法做到这一点? How to pass a customer object to getCustomer() endpoint.如何将客户对象传递给 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;
        }

Edited已编辑

In GET methods, the data is sent in the URL.GET方法中,数据在 URL 中发送。 just like: http://www.test.com/users/1就像: http : //www.test.com/users/1

In POST methods, The data is stored in the request body of the HTTP request.POST方法中,数据存储在 HTTP 请求的请求体中。

Therefore we should not expect .get() method to have .bodyValue().因此,我们不应期望 .get() 方法具有 .bodyValue()。

Now if you wanna send data using GET method, you should send them in the URL, like below snippet现在如果你想使用 GET 方法发送数据,你应该在 URL 中发送它们,如下面的代码片段

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

Useful Spring webClient sample:有用的 Spring webClient 示例:

spring 5 WebClient and WebTestClient Tutorial with Examples spring 5 WebClient 和 WebTestClient 教程与示例

Further information about POST and GET有关 POST 和 GET 的更多信息

HTTP Request Methods HTTP 请求方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM