@@ -132,6 +132,110 @@ headers.set("Authorization", "Bearer your_token");
132
132
![ alt text] ( /Images/springbootexternalapi/image-3.png )
133
133
134
134
- ` delete(url) ` sends a DELETE request without returning any response body
135
+
136
+ ## ` WebClient `
137
+
138
+ - ` WebClient ` is a modern, ** non-blocking HTTP client** introduced in the Spring ` WebFlux ` module. It is designed to handle reactive and asynchronous programming models. Unlike the ** older ` RestTemplate ` , which operates synchronously** , ` WebClient ` is ** fully asynchronous and leverages reactive streams** , making it suitable for high-concurrency, low-latency applications
139
+ - Lets first download the dependency
140
+
141
+ ```
142
+ <dependency>
143
+ <groupId>org.springframework.boot</groupId>
144
+ <artifactId>spring-boot-starter-webflux</artifactId>
145
+ </dependency>
146
+ ```
147
+
148
+ - Lets configure webflux
149
+
150
+ ```
151
+ package com.api.api;
152
+ import org.springframework.context.annotation.Bean;
153
+ import org.springframework.context.annotation.Configuration;
154
+ import org.springframework.web.client.RestTemplate;
155
+ import org.springframework.web.reactive.function.client.WebClient;
156
+
157
+ @Configuration
158
+ public class ApiConfiguration {
159
+ @Bean
160
+ public RestTemplate restTemplate() {
161
+ return new RestTemplate();
162
+ }
163
+
164
+ @Bean
165
+ public WebClient webClient() {
166
+ return WebClient.builder().baseUrl("https://jsonplaceholder.typicode.com/").build();
167
+ }
168
+ }
169
+ ```
170
+
171
+ - Lets set up ` GET ` and ` POST ` method
172
+
173
+ ```
174
+ package com.api.api;
175
+
176
+ import org.springframework.beans.factory.annotation.Autowired;
177
+ import org.springframework.http.HttpEntity;
178
+ import org.springframework.http.HttpHeaders;
179
+ import org.springframework.http.MediaType;
180
+ import org.springframework.http.ResponseEntity;
181
+ import org.springframework.web.bind.annotation.DeleteMapping;
182
+ import org.springframework.web.bind.annotation.GetMapping;
183
+ import org.springframework.web.bind.annotation.PathVariable;
184
+ import org.springframework.web.bind.annotation.PostMapping;
185
+ import org.springframework.web.bind.annotation.RequestBody;
186
+ import org.springframework.web.bind.annotation.RestController;
187
+ import org.springframework.web.client.RestTemplate;
188
+ import org.springframework.web.reactive.function.client.WebClient;
189
+
190
+ import reactor.core.publisher.Mono;
191
+
192
+ @RestController
193
+ public class ExternalApiCall {
194
+
195
+ @Autowired
196
+ private WebClient webClient;
197
+
198
+ @GetMapping("/webclient/get")
199
+ public Mono<String> getJsonDataViaWebFlux() {
200
+ return webClient.get()
201
+ .uri("/posts")
202
+ .retrieve()
203
+ .bodyToMono(String.class);
204
+ }
205
+
206
+ @PostMapping("/webclient/post")
207
+ public Mono<String> postJsonDataViaWebFlux(@RequestBody String requestBody) {
208
+
209
+ return webClient.post()
210
+ .uri("/posts")
211
+ .header("Content-Type", "application/json")
212
+ .bodyValue(requestBody)
213
+ .retrieve()
214
+ .bodyToMono(String.class);
215
+ }
216
+
217
+ }
218
+ ```
219
+
220
+ - So when we hit the endpoint we get the response
221
+
222
+ ![ alt text] ( Images/springbootexternalapi/image-4.png )
223
+
224
+ ![ alt text] ( Images/springbootexternalapi/image-5.png )
225
+
226
+
227
+ | ** Feature** | ** RestTemplate** | ** WebClient** |
228
+ | ---------------------------| ---------------------------------------| -----------------------------------------|
229
+ | ** Blocking/Non-blocking** | Blocking (synchronous). | Non-blocking (asynchronous). |
230
+ | ** Programming Paradigm** | Imperative programming. | Reactive programming. |
231
+ | ** Performance** | Better for low-concurrency scenarios. | Scales well for high-concurrency. |
232
+ | ** Thread Usage** | Blocks threads while waiting for IO. | Frees up threads, better resource use. |
233
+ | ** Scalability** | Limited by thread pool size. | Highly scalable for large workloads. |
234
+ | ** Return Type** | Direct objects (e.g., String). | Reactive types (Mono, Flux). |
235
+ | ** Complexity** | Simple to use. | Requires knowledge of reactive streams. |
236
+ | ** Spring Framework** | Part of Spring MVC. | Part of Spring WebFlux. |
237
+
238
+
135
239
- Lets learn about [ SpringBoot without Maven] ( https://github.com/codophilic/LearnSpringBoot/blob/main/SpringBoot%20Without%20Maven.md )
136
240
137
241
0 commit comments