Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏

第 7 章 WebFlux framework

目录

7.1. Getting Started
7.1.1. Maven
7.1.2. Application
7.1.3. RestController
7.1.4. 测试
7.2. WebFlux 与 SprintMVC 有什么不同?
7.2.1. 实验程序
7.2.2. 实验结果
7.3. WebFlux Router
7.3.1. Component 原件
7.3.2. 路由配置
7.4. RestController
7.4.1. Post 数据
7.4.2. Thymeleaf
7.4.3. Mono
7.4.4. Flux 返回多条数据
7.4.5. SSE
7.4.6. 末尾连接字符串
7.4.7. Flux scan
7.4.8. ConnectableFlux
7.4.9. 上传文件并保存
7.5. Newline Delimited JSON (NDJSON)
7.6. WebClient
7.6.1. 配置 WebClient
7.6.2. @Controller/@RestController 实例
7.6.3. Get 请求实例
7.6.4. URI 参数
7.6.5. 查询参数
7.6.6. Post 操作演示
7.6.7. Post 表单数据
7.6.8. 上传文件
7.6.9. 设置 HTTP 头
7.6.10. 同步阻塞等待返回结果
7.6.11. websocket
7.6.12. 获取 HTTP 链接状态
7.6.13. Http Base Authentication - 401 Unauthorized
7.6.14. SSE(Server-Sent Events)
7.6.15. 超时时间
7.6.16. share() 共享订阅数据
7.6.17. 打印调试日志
7.6.18. 解决 WebClient 截断 URI的问题
7.6.19. 异常处理
7.7. Webflux 安全
7.7.1. Token 拦截器
7.7.2. JWT
7.7.3. spring-boot-starter-security
7.8. R2DBC
7.8.1. MySQL
7.8.2. R2dbcEntityTemplate
7.8.3. MySQL Point 数据类型
7.9. Webflux Redis
7.9.1. Maven Redis 依赖
7.9.2. Redis 配置
7.9.3. Config
7.9.4. Service
7.9.5. RestController
7.10. Webflux Mongdb
7.10.1. Maven 依赖
7.10.2. Repository
7.10.3. Service
7.10.4. 控制器
7.11. 常见问题
7.11.1. The Java/XML config for Spring MVC and Spring WebFlux cannot both be enabled, e.g. via @EnableWebMvc and @EnableWebFlux, in the same application.
7.11.2. @EnableWebFluxSecurity 与 @EnableReactiveMethodSecurity 不生效
7.11.3. webflux netty 不支持 Content-Type: application/x-www-form-urlencoded

7.1. Getting Started

7.1.1. Maven

    		
<?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>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.1.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>cn.netkiller</groupId>
	<artifactId>webflux</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>webflux</name>
	<description>Demo webflux project for Spring Boot</description>

	<properties>
		<java.version>11</java.version>
	</properties>

	<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.springframework.restdocs</groupId>
			<artifactId>spring-restdocs-mockmvc</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>
    		
    		
			

7.1.2. Application

    		
package cn.netkiller.webflux;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class WebfluxApplication {

	public static void main(String[] args) {
		SpringApplication.run(WebfluxApplication.class, args);
	}

}
    		
    		
			

7.1.3. RestController

    		
package cn.netkiller.webflux;

import org.reactivestreams.Publisher;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import reactor.core.publisher.Mono;

@RestController
public class TestController {

	public TestController() {

	}

	@GetMapping("/")
	@ResponseBody
	public Publisher<String> index() {
		return Mono.just("Hello world!");
	}

}
    		
    		
			

7.1.4. 测试

			
neo@MacBook-Pro ~/webflux % mvn spring-boot:run			
			
			
			
neo@MacBook-Pro ~ % curl http://localhost:8080
Hello world!%