Skip to content

Commit e9dbe98

Browse files
committed
WebFlux
1 parent b33c7c7 commit e9dbe98

File tree

6 files changed

+139
-2
lines changed

6 files changed

+139
-2
lines changed
286 KB
Loading
273 KB
Loading

SpringBoot and External API.md

+104
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,110 @@ headers.set("Authorization", "Bearer your_token");
132132
![alt text](/Images/springbootexternalapi/image-3.png)
133133

134134
- `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+
135239
- Lets learn about [SpringBoot without Maven](https://github.com/codophilic/LearnSpringBoot/blob/main/SpringBoot%20Without%20Maven.md)
136240

137241

SpringBoot and External API/api/pom.xml

+4-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@
3838
<groupId>org.springframework.boot</groupId>
3939
<artifactId>spring-boot-starter-web-services</artifactId>
4040
</dependency>
41-
41+
<dependency>
42+
<groupId>org.springframework.boot</groupId>
43+
<artifactId>spring-boot-starter-webflux</artifactId>
44+
</dependency>
4245
<dependency>
4346
<groupId>org.springframework.boot</groupId>
4447
<artifactId>spring-boot-devtools</artifactId>

SpringBoot and External API/api/src/main/java/com/api/api/ApiConfiguration.java

+6
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@
22
import org.springframework.context.annotation.Bean;
33
import org.springframework.context.annotation.Configuration;
44
import org.springframework.web.client.RestTemplate;
5+
import org.springframework.web.reactive.function.client.WebClient;
56

67
@Configuration
78
public class ApiConfiguration {
89
@Bean
910
public RestTemplate restTemplate() {
1011
return new RestTemplate();
1112
}
13+
14+
@Bean
15+
public WebClient webClient() {
16+
return WebClient.builder().baseUrl("https://jsonplaceholder.typicode.com").build();
17+
}
1218
}
1319

SpringBoot and External API/api/src/main/java/com/api/api/ExternalApiCall.java

+25-1
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,20 @@
1010
import org.springframework.web.bind.annotation.PathVariable;
1111
import org.springframework.web.bind.annotation.PostMapping;
1212
import org.springframework.web.bind.annotation.RequestBody;
13-
import org.springframework.web.bind.annotation.RequestParam;
1413
import org.springframework.web.bind.annotation.RestController;
1514
import org.springframework.web.client.RestTemplate;
15+
import org.springframework.web.reactive.function.client.WebClient;
16+
17+
import reactor.core.publisher.Mono;
1618

1719
@RestController
1820
public class ExternalApiCall {
1921

2022
@Autowired
2123
private RestTemplate restTemplate;
24+
25+
@Autowired
26+
private WebClient webClient;
2227

2328
@GetMapping("/restTemplate/get")
2429
public ResponseEntity<String> getJsonData() {
@@ -45,4 +50,23 @@ public String deleteJsonData(@PathVariable String id) {
4550
}
4651

4752

53+
@GetMapping("/webclient/get")
54+
public Mono<String> getJsonDataViaWebFlux() {
55+
return webClient.get()
56+
.uri("/posts")
57+
.retrieve()
58+
.bodyToMono(String.class);
59+
}
60+
61+
@PostMapping("/webclient/post")
62+
public Mono<String> postJsonDataViaWebFlux(@RequestBody String requestBody) {
63+
64+
return webClient.post()
65+
.uri("/posts")
66+
.header("Content-Type", "application/json")
67+
.bodyValue(requestBody)
68+
.retrieve()
69+
.bodyToMono(String.class);
70+
}
71+
4872
}

0 commit comments

Comments
 (0)