時間:2018年04月08日星期日
說明:本文部份內容均來自慕課網。@慕課網:https://www.imooc.com
教學源碼:無
學習源碼:https://github.com/zccodere/s...java
課程內容react
2.0 新特性git
爲何選擇Web Fluxgithub
開發環境web
簡單應用spring
場景說明apache
代碼編寫編程
1.建立名爲boot-simple的maven工程pom以下oracle
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.myimooc</groupId> <artifactId>boot-simple</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>boot-simple</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2.完成後的項目結構以下app
3.編寫BootSimpleApplication類
package com.myimooc.boot.simple; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * <br> * 標題: 啓動類<br> * 描述: 簡單應用<br> * * @author zc * @date 2018/04/08 */ @SpringBootApplication public class BootSimpleApplication { public static void main(String[] args) { SpringApplication.run(BootSimpleApplication.class, args); } }
4.編寫User類
package com.myimooc.boot.simple.domain; import java.io.Serializable; /** * <br> * 標題: 用戶模型<br> * 描述: POJO類<br> * * @author zc * @date 2018/04/08 */ public class User implements Serializable{ private static final long serialVersionUID = 7564986345486454458L; /** * 主鍵ID */ private Integer id; /** * 用戶名稱 */ private String name; @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + '}'; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
5.編寫UserRepository類
package com.myimooc.boot.simple.repository; import com.myimooc.boot.simple.domain.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; /** * <br> * 標題: 用戶資源庫<br> * 描述: 領域驅動模型開發<br> * * @author zc * @date 2018/04/08 * {@link User} {@link Repository} */ @Repository public class UserRepository { /** * 採用內存型存儲方式 */ private final ConcurrentMap<Integer,User> repository = new ConcurrentHashMap<>(16); private final static AtomicInteger ID_GENERATOR = new AtomicInteger(); /** * 保存用戶對象 * @param user {@link User} * @return 若是成功返回<code>true</code>,不然,返回<code>false</code> */ public boolean save(User user){ // ID 從 1 開始 Integer id = ID_GENERATOR.incrementAndGet(); user.setId(id); // put 方法返回 null 時表示保存成功 return repository.put(id,user) == null; } /** * 返回全部用戶列表 * @return 全部用戶列表 */ public Collection<User> findAll(){ return repository.values(); } }
6.編寫UserController類
package com.myimooc.boot.simple.controller; import com.myimooc.boot.simple.domain.User; import com.myimooc.boot.simple.repository.UserRepository; 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; /** * <br> * 標題: 用戶 REST 接口<br> * 描述: RESTFul 格式<br> * * @author zc * @date 2018/04/08 */ @RestController public class UserController { private final UserRepository userRepository; @Autowired public UserController(UserRepository userRepository){ this.userRepository = userRepository; } @PostMapping("/person/save") public User save(@RequestParam String name){ User user = new User(); user.setName(name); if (userRepository.save(user)){ System.out.printf("用戶對象: %s 保存成功\n",user); } return user; } }
7.編寫RouterFunctionConfiguration類
package com.myimooc.boot.simple.config; import com.myimooc.boot.simple.domain.User; import com.myimooc.boot.simple.repository.UserRepository; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.reactive.function.server.*; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import java.util.Collection; /** * <br> * 標題: 路由器函數配置<br> * 描述: 配置映射<br> * * @author zc * @date 2018/04/08 */ @Configuration public class RouterFunctionConfiguration { /** * Servlet 規範 * 請求接口:ServletRequest 或者 HttpServletRequest * 響應接口:ServletResponse 或者 HttpServletResponse * * Spring 5.0 從新定義了服務請求的響應接口 * 請求接口:ServerRequest * 響應接口:ServerResponse * 便可支持 Servlet 規範,也能夠支持自定義,好比 Netty (Web Server) * 以本例 * 定義 GET 請求,而且放回全部的用戶對象 URI:/person/find/all * Flux 是 0 - N 個對象集合 * Mono 是 0 - 1 個對象集合 * Reactive 中的 Flux 或者 Mono 是異步處理(非阻塞) * 集合對象基本上是同步處理(阻塞) * Flux 或者 Mono 都是 Publisher */ @Bean public RouterFunction<ServerResponse> personFindAll(UserRepository userRepository){ Collection<User> users = userRepository.findAll(); return RouterFunctions.route(RequestPredicates.GET("/person/find/all"), request -> { Flux<User> userFlux = Flux.fromIterable(users); return ServerResponse.ok().body(userFlux,User.class); }); } }
多模塊重構
完成後的結構以下
主要內容