├── .gitignore
├── README.md
├── pom.xml
└── src
├── main
├── java
│ └── com
│ │ └── example
│ │ └── demospringbootwebflux
│ │ ├── DemoSpringBootWebfluxApplication.java
│ │ ├── RoutingConfiguration.java
│ │ ├── User.java
│ │ ├── UserHandler.java
│ │ ├── UserRepository.java
│ │ └── UserRepositoryImpl.java
└── resources
│ └── application.properties
└── test
└── java
└── com
└── example
└── demospringbootwebflux
└── DemoSpringBootWebfluxApplicationTests.java
/.gitignore:
--------------------------------------------------------------------------------
1 | target/
2 | !.mvn/wrapper/maven-wrapper.jar
3 |
4 | ### STS ###
5 | .apt_generated
6 | .classpath
7 | .factorypath
8 | .project
9 | .settings
10 | .springBeans
11 |
12 | ### IntelliJ IDEA ###
13 | .idea
14 | *.iws
15 | *.iml
16 | *.ipr
17 |
18 | ### NetBeans ###
19 | nbproject/private/
20 | build/
21 | nbbuild/
22 | dist/
23 | nbdist/
24 | .nb-gradle/
25 |
26 | .mvn
27 | mvnw
28 | mvnw.cmd
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## References
2 |
3 | - http://javasampleapproach.com/spring-framework/spring-boot/springboot-webflux-functional-restapis
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | com.example
7 | demo-spring-boot-webflux
8 | 0.0.1-SNAPSHOT
9 | jar
10 |
11 | demo-spring-boot-webflux
12 | Demo project for Spring Boot
13 |
14 |
15 | org.springframework.boot
16 | spring-boot-starter-parent
17 | 2.0.0.M7
18 |
19 |
20 |
21 |
22 | UTF-8
23 | UTF-8
24 | 1.8
25 |
26 |
27 |
28 |
29 | org.springframework.boot
30 | spring-boot-starter-webflux
31 |
32 |
33 |
34 | org.springframework.boot
35 | spring-boot-starter-test
36 | test
37 |
38 |
39 | io.projectreactor
40 | reactor-test
41 | test
42 |
43 |
44 |
45 |
46 |
47 |
48 | org.springframework.boot
49 | spring-boot-maven-plugin
50 |
51 |
52 |
53 |
54 |
55 |
56 | spring-snapshots
57 | Spring Snapshots
58 | https://repo.spring.io/snapshot
59 |
60 | true
61 |
62 |
63 |
64 | spring-milestones
65 | Spring Milestones
66 | https://repo.spring.io/milestone
67 |
68 | false
69 |
70 |
71 |
72 |
73 |
74 |
75 | spring-snapshots
76 | Spring Snapshots
77 | https://repo.spring.io/snapshot
78 |
79 | true
80 |
81 |
82 |
83 | spring-milestones
84 | Spring Milestones
85 | https://repo.spring.io/milestone
86 |
87 | false
88 |
89 |
90 |
91 |
92 |
93 |
94 |
--------------------------------------------------------------------------------
/src/main/java/com/example/demospringbootwebflux/DemoSpringBootWebfluxApplication.java:
--------------------------------------------------------------------------------
1 | package com.example.demospringbootwebflux;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class DemoSpringBootWebfluxApplication {
8 |
9 | public static void main(String[] args) {
10 | SpringApplication.run(DemoSpringBootWebfluxApplication.class, args);
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/src/main/java/com/example/demospringbootwebflux/RoutingConfiguration.java:
--------------------------------------------------------------------------------
1 | package com.example.demospringbootwebflux;
2 |
3 | import org.springframework.context.annotation.Bean;
4 | import org.springframework.context.annotation.Configuration;
5 | import org.springframework.http.MediaType;
6 | import org.springframework.web.reactive.function.server.RouterFunction;
7 | import org.springframework.web.reactive.function.server.ServerResponse;
8 |
9 | import static org.springframework.web.reactive.function.server.RequestPredicates.*;
10 | import static org.springframework.web.reactive.function.server.RouterFunctions.route;
11 |
12 | @Configuration
13 | public class RoutingConfiguration {
14 |
15 | @Bean
16 | public RouterFunction monoRouterFunction(UserHandler userHandler) {
17 | return route(GET("/api/user/index").and(accept(MediaType.APPLICATION_JSON)), userHandler::getAll)
18 | .andRoute(GET("/api/user/{id}").and(accept(MediaType.APPLICATION_JSON)), userHandler::getUser)
19 | .andRoute(POST("/api/user/post").and(accept(MediaType.APPLICATION_JSON)), userHandler::postUser)
20 | .andRoute(PUT("/api/user/put/{id}").and(accept(MediaType.APPLICATION_JSON)), userHandler::putUser)
21 | .andRoute(DELETE("/api/user/delete/{id}").and(accept(MediaType.APPLICATION_JSON)), userHandler::deleteUser);
22 | }
23 |
24 | }
--------------------------------------------------------------------------------
/src/main/java/com/example/demospringbootwebflux/User.java:
--------------------------------------------------------------------------------
1 | package com.example.demospringbootwebflux;
2 |
3 | public class User {
4 | private long id;
5 | private String firstname;
6 | private String lastname;
7 | private int age;
8 |
9 | public User() {
10 | }
11 |
12 | public User(long id, String firstname, String lastname, int age) {
13 | this.id = id;
14 | this.firstname = firstname;
15 | this.lastname = lastname;
16 | this.age = age;
17 | }
18 |
19 | public long getId() {
20 | return id;
21 | }
22 |
23 | public void setId(Long id) {
24 | this.id = id;
25 | }
26 |
27 | public String getFirstname() {
28 | return firstname;
29 | }
30 |
31 | public void setFirstname(String firstname) {
32 | this.firstname = firstname;
33 | }
34 |
35 | public String getLastname() {
36 | return lastname;
37 | }
38 |
39 | public void setLastname(String lastname) {
40 | this.lastname = lastname;
41 | }
42 |
43 | public int getAge() {
44 | return age;
45 | }
46 |
47 | public void setAge(int age) {
48 | this.age = age;
49 | }
50 |
51 | @Override
52 | public String toString() {
53 | String info = String.format("id = %d, firstname = %s, lastname = %s, age = %d", id, firstname, lastname, age);
54 | return info;
55 | }
56 | }
--------------------------------------------------------------------------------
/src/main/java/com/example/demospringbootwebflux/UserHandler.java:
--------------------------------------------------------------------------------
1 | package com.example.demospringbootwebflux;
2 |
3 | import com.example.demospringbootwebflux.User;
4 | import com.example.demospringbootwebflux.UserRepository;
5 | import org.springframework.http.MediaType;
6 | import org.springframework.stereotype.Component;
7 | import org.springframework.web.reactive.function.server.ServerRequest;
8 | import org.springframework.web.reactive.function.server.ServerResponse;
9 | import reactor.core.publisher.Flux;
10 | import reactor.core.publisher.Mono;
11 |
12 | import static org.springframework.web.reactive.function.BodyInserters.fromObject;
13 |
14 | @Component
15 | public class UserHandler {
16 |
17 | private final UserRepository customerRepository;
18 |
19 | public UserHandler(UserRepository repository) {
20 | this.customerRepository = repository;
21 | }
22 |
23 | /**
24 | * GET ALL Users
25 | */
26 | public Mono getAll(ServerRequest request) {
27 | // fetch all customers from repository
28 | Flux customers = customerRepository.getAllUsers();
29 |
30 | // build response
31 | return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(customers, User.class);
32 | }
33 |
34 | /**
35 | * GET a User by ID
36 | */
37 | public Mono getUser(ServerRequest request) {
38 | // parse path-variable
39 | long customerId = Long.valueOf(request.pathVariable("id"));
40 |
41 | // build notFound response
42 | Mono notFound = ServerResponse.notFound().build();
43 |
44 | // get customer from repository
45 | Mono customerMono = customerRepository.getUserById(customerId);
46 |
47 | // build response
48 | return customerMono
49 | .flatMap(customer -> ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(fromObject(customer)))
50 | .switchIfEmpty(notFound);
51 | }
52 |
53 | /**
54 | * POST a User
55 | */
56 | public Mono postUser(ServerRequest request) {
57 | Mono customer = request.bodyToMono(User.class);
58 | return ServerResponse.ok().build(customerRepository.saveUser(customer));
59 | }
60 |
61 | /**
62 | * PUT a User
63 | */
64 | public Mono putUser(ServerRequest request) {
65 | // parse id from path-variable
66 | long customerId = Long.valueOf(request.pathVariable("id"));
67 |
68 | // get customer data from request object
69 | Mono customer = request.bodyToMono(User.class);
70 |
71 | // get customer from repository
72 | Mono responseMono = customerRepository.putUser(customerId, customer);
73 |
74 | // build response
75 | return responseMono
76 | .flatMap(cust -> ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(fromObject(cust)));
77 | }
78 |
79 | /**
80 | * DELETE a User
81 | */
82 | public Mono deleteUser(ServerRequest request) {
83 | // parse id from path-variable
84 | long customerId = Long.valueOf(request.pathVariable("id"));
85 |
86 | // get customer from repository
87 | Mono responseMono = customerRepository.deleteUser(customerId);
88 |
89 | // build response
90 | return responseMono
91 | .flatMap(strMono -> ServerResponse.ok().contentType(MediaType.TEXT_PLAIN).body(fromObject(strMono)));
92 | }
93 |
94 | }
95 |
--------------------------------------------------------------------------------
/src/main/java/com/example/demospringbootwebflux/UserRepository.java:
--------------------------------------------------------------------------------
1 | package com.example.demospringbootwebflux;
2 |
3 | import org.springframework.stereotype.Repository;
4 | import reactor.core.publisher.Flux;
5 | import reactor.core.publisher.Mono;
6 |
7 | public interface UserRepository {
8 | public Mono getUserById(Long id);
9 |
10 | public Flux getAllUsers();
11 |
12 | public Mono saveUser(Mono user);
13 |
14 | public Mono putUser(Long id, Mono user);
15 |
16 | public Mono deleteUser(Long id);
17 | }
--------------------------------------------------------------------------------
/src/main/java/com/example/demospringbootwebflux/UserRepositoryImpl.java:
--------------------------------------------------------------------------------
1 | package com.example.demospringbootwebflux;
2 |
3 | import org.springframework.stereotype.Repository;
4 | import reactor.core.publisher.Flux;
5 | import reactor.core.publisher.Mono;
6 |
7 | import javax.annotation.PostConstruct;
8 | import java.util.HashMap;
9 | import java.util.Map;
10 |
11 | @Repository
12 | public class UserRepositoryImpl implements UserRepository {
13 | private Map users = new HashMap();
14 |
15 | @PostConstruct
16 | public void init() throws Exception {
17 | users.put(Long.valueOf(1), new User(1, "Jack", "Smith", 20));
18 | users.put(Long.valueOf(2), new User(2, "Peter", "Johnson", 25));
19 | }
20 |
21 | @Override
22 | public Mono getUserById(Long id) {
23 | return Mono.just(users.get(id));
24 | }
25 |
26 | @Override
27 | public Flux getAllUsers() {
28 | return Flux.fromIterable(this.users.values());
29 | }
30 |
31 | @Override
32 | public Mono saveUser(Mono monoUser) {
33 | Mono userMono = monoUser.doOnNext(user -> {
34 | // do post
35 | users.put(user.getId(), user);
36 |
37 | // log on console
38 | System.out.println("########### POST:" + user);
39 | });
40 |
41 | return userMono.then();
42 | }
43 |
44 | @Override
45 | public Mono putUser(Long id, Mono monoUser) {
46 | Mono userMono = monoUser.doOnNext(user -> {
47 | // reset user.Id
48 | user.setId(id);
49 |
50 | // do put
51 | users.put(id, user);
52 |
53 | // log on console
54 | System.out.println("########### PUT:" + user);
55 | });
56 |
57 | return userMono;
58 | }
59 |
60 | @Override
61 | public Mono deleteUser(Long id) {
62 | // delete processing
63 | users.remove(id);
64 | return Mono.just("Delete Succesfully!");
65 | }
66 | }
--------------------------------------------------------------------------------
/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.main.banner-mode=off
2 | server.port=8080
3 | server.address=192.168.0.201
--------------------------------------------------------------------------------
/src/test/java/com/example/demospringbootwebflux/DemoSpringBootWebfluxApplicationTests.java:
--------------------------------------------------------------------------------
1 | package com.example.demospringbootwebflux;
2 |
3 | import org.junit.Test;
4 | import org.junit.runner.RunWith;
5 | import org.springframework.boot.test.context.SpringBootTest;
6 | import org.springframework.test.context.junit4.SpringRunner;
7 |
8 | @RunWith(SpringRunner.class)
9 | @SpringBootTest
10 | public class DemoSpringBootWebfluxApplicationTests {
11 |
12 | @Test
13 | public void contextLoads() {
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------