In this post under Spring WebClient, I will show with example how to send a user defined object as part of POST request.
In all my previous post under Spring WebClient, I used to send data of type String class as part of POST request. Now in this example I will send a user defined object instead of String object. There
is no difference between the two approaches. You have to place user defined object whereever String object was used.
For our example I will use the below Java class whose instance will be sent as part of POST request.
Post class
package defaultPackage;
public class Post {
private int id;
private String title;
private String body;
private int userId;
public Post() {
}
public Post(String title, String body, int userId) {
this.title = title;
this.body = body;
this.userId = userId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
...
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(this.id).append(":");
stringBuilder.append(this.body).append(":");
stringBuilder.append(this.title).append(":");
stringBuilder.append(this.userId);
return stringBuilder.toString();
}
}
In the above “Post” class, we have 4 private fields (i.e., id, title, body, userId) with their getter and setters. I have override the “toString” method.
Now lets see the main class where we will use the WebClient to send a post request.
Main class
1 package defaultPackage;
2
3 import org.springframework.web.reactive.function.client.ClientResponse;
4 import org.springframework.web.reactive.function.client.WebClient;
5 import reactor.core.publisher.Mono;
6
7 public class Example8 {
8 public static void main(String[] args) throws Exception {
9 Post post = new Post("morpheus", "leader", 1);
10
11 WebClient webClient = WebClient.create("https://jsonplaceholder.typicode.com");
12 Mono<Post> response = webClient.post()
13 .uri("/posts")
14 .bodyValue(post)
15 .retrieve()
16 .bodyToMono(Post.class);
17 response.subscribe(value -> {
18 System.out.println(value);
19 });
20 Thread.sleep(3000);
21 }
22 }
In the above code, at line 9 I create an instance of “Post” class.
At line 11, I create an instance of “WebClient” using the static method “create” and passing the url as parameter.
The url “jsonplaceholder.typicode.com/posts” accepts a “Post” object serialized in JSON format as part of request body. On successful completion of the request it returns a “Post” object as part of response body with “id” field populated.
At line 12, I perform the below operations
1) Configuring the WebClient instance for POST request by calling “post” method
2) Configuring the remaining url by calling “uri” method
3) Configuring the data to be posted by calling “bodyValue” method and passing “Post” object as an argument to the method.
4) Calling the “retrieve” method to get the response body
5) Calling the “bodyToMono” method to convert the body to an instance of “Mono<Post>” class.
At line 17, I am calling the “subscribe” method of “Mono” instance and passing a functional interface which will print the value to the console.
When the “subscribe” method is called, the WebClient framework makes an actual api call to the server.
When the post is successful, we get the “Post” object as a reponse with its “id” field set to non null value.
Below is the output for your reference
For our example, at line 20, I wait for 3 sec to get the response. This is not required in production code.
In this way we can make an POST HTTP call to send a user defined object as request body.
Output
101:leader:morpheus:1