Lisa
Lisa

Reputation: 169

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? 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.

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;
        }

Upvotes: 0

Views: 461

Answers (1)

Mehrdad Yami
Mehrdad Yami

Reputation: 1681

Edited

In GET methods, the data is sent in the URL. just like: http://www.test.com/users/1

In POST methods, The data is stored in the request body of the HTTP request.

Therefore we should not expect .get() method to have .bodyValue().

Now if you wanna send data using GET method, you should send them in the URL, like below snippet

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

Useful Spring webClient sample:

spring 5 WebClient and WebTestClient Tutorial with Examples

Further information about POST and GET

HTTP Request Methods

Upvotes: 1

Related Questions