Skip to content

Commit 57dfc89

Browse files
author
zhangtao
committed
WebFlux 新增加的几种方法,以及SSE模式等响应式编程
1 parent 43f8db7 commit 57dfc89

File tree

8 files changed

+257
-0
lines changed

8 files changed

+257
-0
lines changed

SpringWebFluxDemo/pom.xml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.1.3.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>com.github.xiaour</groupId>
12+
<artifactId>flux</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>SpringWebFluxDemo</name>
15+
<description>Demo project for Spring Boot</description>
16+
17+
<properties>
18+
<java.version>1.8</java.version>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-web</artifactId>
25+
</dependency>
26+
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-webflux</artifactId>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-test</artifactId>
35+
<scope>test</scope>
36+
</dependency>
37+
</dependencies>
38+
39+
<build>
40+
<plugins>
41+
<plugin>
42+
<groupId>org.springframework.boot</groupId>
43+
<artifactId>spring-boot-maven-plugin</artifactId>
44+
</plugin>
45+
</plugins>
46+
</build>
47+
48+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.github.xiaour.flux;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class SpringWebFluxDemoApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(SpringWebFluxDemoApplication.class, args);
11+
}
12+
13+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.github.xiaour.flux.controller;
2+
3+
import org.springframework.web.bind.annotation.GetMapping;
4+
import org.springframework.web.bind.annotation.RequestMapping;
5+
import org.springframework.web.bind.annotation.RestController;
6+
import reactor.core.publisher.Mono;
7+
8+
/**
9+
* @Author: [email protected]
10+
* @Date: 2019-02-27 16:26
11+
* @version: v1.0
12+
* @Description: 入门案例
13+
*/
14+
15+
@RestController
16+
@RequestMapping("/")
17+
public class HelloWorlController {
18+
19+
@GetMapping("hello")
20+
public Mono<String> hello(){
21+
return Mono.just("hello World!");
22+
}
23+
24+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.github.xiaour.flux.controller;
2+
3+
import io.netty.util.internal.ThreadLocalRandom;
4+
import org.springframework.http.codec.ServerSentEvent;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
import reactor.core.publisher.Flux;
9+
import reactor.util.function.Tuples;
10+
11+
import java.time.Duration;
12+
13+
/**
14+
* @Author: [email protected]
15+
* @Date: 2019-02-27 16:37
16+
* @version: v1.0
17+
* @Description: 服务器推送接口
18+
*/
19+
@RestController
20+
@RequestMapping("/sse")
21+
public class SSEController {
22+
23+
@GetMapping("/randomNumbers")
24+
public Flux<ServerSentEvent<Integer>> randomNumbers() {
25+
return Flux.interval(Duration.ofSeconds(2))
26+
.map(seq -> Tuples.of(seq, ThreadLocalRandom.current().nextInt()))
27+
.map(data -> ServerSentEvent.<Integer>builder()
28+
.event("random")
29+
.id(Long.toString(data.getT1()))
30+
.data(data.getT2())
31+
.build());
32+
}
33+
34+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.github.xiaour.flux.controller;
2+
3+
import com.github.xiaour.flux.entity.User;
4+
import com.github.xiaour.flux.service.UserService;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.web.bind.annotation.*;
7+
import reactor.core.publisher.Flux;
8+
import reactor.core.publisher.Mono;
9+
10+
import java.util.Objects;
11+
12+
/**
13+
* @Author: [email protected]
14+
* @Date: 2019-02-27 16:33
15+
* @version: v1.0
16+
* @Description: 用户管理接口
17+
*/
18+
@RestController
19+
@RequestMapping("/user")
20+
public class UserController {
21+
22+
@Autowired
23+
private UserService userService;
24+
25+
26+
@GetMapping("")
27+
public Flux<User> list() {
28+
return userService.list();
29+
}
30+
31+
@GetMapping("/{id}")
32+
public Mono<User> getById(@PathVariable("id") final String id) {
33+
return this.userService.getById(id);
34+
}
35+
36+
@PostMapping("")
37+
public Mono<User> create(@RequestBody final User user) {
38+
return this.userService.createOrUpdate(user);
39+
}
40+
41+
@PutMapping("/{id}")
42+
public Mono<User> update(@PathVariable("id") final String id, @RequestBody final User user) {
43+
Objects.requireNonNull(user);
44+
user.setId(id);
45+
return this.userService.createOrUpdate(user);
46+
}
47+
48+
@DeleteMapping("/{id}")
49+
public Mono<User> delete(@PathVariable("id") final String id) {
50+
return this.userService.delete(id);
51+
}
52+
53+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.github.xiaour.flux.entity;
2+
3+
/**
4+
* @Author: [email protected]
5+
* @Date: 2019-02-27 16:31
6+
* @version: v1.0
7+
* @Description:
8+
*/
9+
public class User {
10+
11+
private String id;
12+
13+
private String name;
14+
15+
private Integer age;
16+
17+
public String getId() {
18+
return id;
19+
}
20+
21+
public void setId(String id) {
22+
this.id = id;
23+
}
24+
25+
public String getName() {
26+
return name;
27+
}
28+
29+
public void setName(String name) {
30+
this.name = name;
31+
}
32+
33+
public Integer getAge() {
34+
return age;
35+
}
36+
37+
public void setAge(Integer age) {
38+
this.age = age;
39+
}
40+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.github.xiaour.flux.service;
2+
3+
import com.github.xiaour.flux.entity.User;
4+
import org.springframework.stereotype.Service;
5+
import reactor.core.publisher.Flux;
6+
import reactor.core.publisher.Mono;
7+
8+
import java.util.Map;
9+
import java.util.concurrent.ConcurrentHashMap;
10+
11+
/**
12+
* @Author: [email protected]
13+
* @Date: 2019-02-27 16:30
14+
* @version: v1.0
15+
* @Description:
16+
*/
17+
@Service
18+
public class UserService {
19+
20+
private final Map<String, User> data = new ConcurrentHashMap<>();
21+
22+
public Flux<User> list() {
23+
return Flux.fromIterable(this.data.values());
24+
}
25+
26+
public Flux<User> getById(final Flux<String> ids) {
27+
return ids.flatMap(id -> Mono.justOrEmpty(this.data.get(id)));
28+
}
29+
30+
public Mono<User> getById(final String id) {
31+
return Mono.justOrEmpty(this.data.get(id));
32+
}
33+
34+
public Mono<User> createOrUpdate(final User user) {
35+
this.data.put(user.getId(), user);
36+
return Mono.just(user);
37+
}
38+
39+
public Mono<User> delete(final String id) {
40+
return Mono.justOrEmpty(this.data.remove(id));
41+
}
42+
43+
44+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

0 commit comments

Comments
 (0)