Skip to content

Commit 3337d2e

Browse files
committed
webflux tutorial
1 parent da7877d commit 3337d2e

File tree

5 files changed

+106
-0
lines changed

5 files changed

+106
-0
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<module>bucket4j-tutorial</module>
4646
<module>aspect-tutorial</module>
4747
<module>messaging-tutorial</module>
48+
<module>reactive-tutorial</module>
4849
</modules>
4950
<scm>
5051
<url>https://github.com/ErwanLT/springboot-demo.git</url>

reactive-tutorial/pom.xml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>fr.eletutour</groupId>
8+
<artifactId>springboot-demo</artifactId>
9+
<version>6.0.1-SNAPSHOT</version>
10+
</parent>
11+
12+
<artifactId>reactive-tutorial</artifactId>
13+
14+
<properties>
15+
<maven.compiler.source>21</maven.compiler.source>
16+
<maven.compiler.target>21</maven.compiler.target>
17+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18+
</properties>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>org.springframework.boot</groupId>
23+
<artifactId>spring-boot-starter-webflux</artifactId>
24+
</dependency>
25+
</dependencies>
26+
27+
</project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package fr.eletutour.reactive;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class ReactiveTutorialApplication {
8+
public static void main(String[] args) {
9+
SpringApplication.run(ReactiveTutorialApplication.class);
10+
}
11+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package fr.eletutour.reactive.controller;
2+
3+
import fr.eletutour.reactive.model.Post;
4+
import org.springframework.http.HttpStatusCode;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.PathVariable;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.RestController;
9+
import org.springframework.web.reactive.function.client.WebClient;
10+
import org.springframework.web.server.ResponseStatusException;
11+
import reactor.core.publisher.Flux;
12+
import reactor.core.publisher.Mono;
13+
import reactor.core.scheduler.Schedulers;
14+
15+
16+
@RestController
17+
@RequestMapping("/api")
18+
public class ReactiveController {
19+
20+
private final WebClient webClient = WebClient.create("https://jsonplaceholder.typicode.com");
21+
22+
@GetMapping("/posts/{id}")
23+
public Mono<Post> getPost(@PathVariable String id) {
24+
return webClient.get()
25+
.uri("/posts/{id}", id)
26+
.retrieve()
27+
.onStatus(HttpStatusCode::isError, response -> Mono.error(new ResponseStatusException(response.statusCode(), "API call failed")))
28+
.bodyToMono(Post.class);
29+
}
30+
31+
@GetMapping("/posts")
32+
public Flux<Post> getPosts() {
33+
return webClient.get()
34+
.uri("/posts")
35+
.retrieve()
36+
.onStatus(HttpStatusCode::isError, response -> Mono.error(new ResponseStatusException(response.statusCode(), "API call failed")))
37+
.bodyToFlux(Post.class);
38+
}
39+
40+
41+
@GetMapping("/posts/{id}/enriched")
42+
public Mono<Post> getEnrichedPost(@PathVariable String id) {
43+
return getPost(id)
44+
.flatMap(post -> Mono.fromCallable(() -> enrichPostBlocking(post))
45+
.subscribeOn(Schedulers.boundedElastic())
46+
);
47+
}
48+
49+
50+
private Post enrichPostBlocking(Post post) {
51+
try {
52+
Thread.sleep(1000);
53+
} catch (InterruptedException e) {
54+
Thread.currentThread().interrupt();
55+
}
56+
return new Post(
57+
post.userId(),
58+
post.id(),
59+
"[Enriched] " + post.title(),
60+
post.body()
61+
);
62+
}
63+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package fr.eletutour.reactive.model;
2+
3+
public record Post(Integer userId, Integer id, String title, String body) {
4+
}

0 commit comments

Comments
 (0)