前言:
简介:
工具:
(1)新建一个springboot工程
(2)pom.xml
    <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
(3)建立实体 User (简化不写了)
(4)建立 repository
package com.lwc.repository;

import com.lwc.pojo.User;
import org.springframework.stereotype.Repository; import java.util.Collection;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicInteger; /**
* @author eddie.lee
* @Package com.lwc.repository
* @ClassName UserRepository
* @description this is dao
* @date created in 2018-06-07 21:36
* @modified by
*/
@Repository
public class UserRespository { /**
* 使用内存方式存储 ===》 Map
*/
private ConcurrentMap<Integer, User> repository = new ConcurrentHashMap<>(); /**
* id生成
*/
private final static AtomicInteger idGenerator = new AtomicInteger(); /**
* 保护用户对象
*
* @param user
* @return 如果保存成功,返回true,否则,返回false
*/
public boolean save(User user) {
// id 从 1 开始
Integer id = idGenerator.incrementAndGet();
// 设置主键
user.setId(id); return repository.put(id, user) == null;
} /**
* 返回所有用户列表
*
* @return
*/
public Collection<User> findAll(){
return repository.values();
} }
(5)建立控制层
package com.lwc.controller;

import com.lwc.pojo.User;
import com.lwc.repository.UserRespository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; /**
* @author eddie.lee
* @Package com.lwc.controller
* @ClassName UserController
* @description
* @date created in 2018-06-07 21:44
* @modified by
*/
@RestController
public class UserController { private final UserRespository userRespository; @Autowired
public UserController(UserRespository userRespository) {
this.userRespository = userRespository;
} @PostMapping("/person/save")
public User save(@RequestParam("name") String name) {
User user = new User();
user.setName(name);
if (userRespository.save(user)) {
System.out.printf("用户对象: %s 保存成功! ", user);
System.out.println(" ");
}
return user;
} }
(6)PostMan测试,插入接口。
(7)使用 Web Flux , Spring  5.0 后提出,优点是NIO
package com.lwc.config;

import com.lwc.pojo.User;
import com.lwc.repository.UserRespository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; import java.util.Collection; /**
* @author eddie.lee
* @Package com.lwc.config
* @ClassName RouterFunctionConfiguration
* @description 路由器函数配置
* @date created in 2018-06-07 22:33
* @modified by
*/
@Configuration
public class RouterFunctionConfiguration { // @Autowired
// private UserRespository userRepository; @Bean
@Autowired
public RouterFunction<ServerResponse> personFindAll(UserRespository userRepository) {
return RouterFunctions.route(RequestPredicates.GET("/person/find/all"),
request -> {
Collection<User> users = userRepository.findAll();
Flux<User> userFlux = Flux.fromIterable(users);
Mono<ServerResponse> body = ServerResponse.ok().body(userFlux, User.class);
return body;
});
} }
05-20 02:53