In this post under Spring WebClient, I will explain with example how to perform simple DELETE HTTP request.
Below is the complete code for your reference.
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
6 import reactor.core.publisher.Mono;
7
8 public class Example4 {
9 public static void main(String[] args) throws Exception {
10 WebClient webClient = WebClient.create("https://jsonplaceholder.typicode.com");
11 Mono<String> response = webClient.delete()
12 .uri("/users/10")
13 .retrieve()
14 .bodyToMono(String.class);
15 response.subscribe(value -> System.out.println(value));
16 Thread.sleep(3000);
17 }
18 }
In the above code at line 10, I create an instance of “WebClient” class by calling static method “create” and passing the base url as a parameter.
From line 11 to 14, I am performing the below operations
1) configuring “WebClient” instance for delete request by calling “delete” method
2) configuring the remaining url by calling “uri” method.
3) calling the “retrieve” method to get the response body
4) calling the “bodyToMono” method to convert the response body to an instance of “Mono<String>” class
At line 15, I call the “subscribe” method. When the “subscribe” method is called, the WebClient framework makes an actual api call to the server.
For our example, at line 16, I wait for 3 sec to get the response. This is not required in production code.
In this way we can post a delete method.