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

第 69 章 WebFlux framework

目录

69.1. Getting Started
69.1.1. Maven
69.1.2. Application
69.1.3. RestController
69.1.4. 测试
69.2. WebFlux 与 SprintMVC 有什么不同?
69.2.1. 实验程序
69.2.2. 实验结果
69.3. WebFlux Router
69.3.1. Component 原件
69.3.2. 路由配置
69.4. RestController
69.4.1. Post 数据
69.4.2. Thymeleaf
69.4.3. Mono
69.4.4. Flux 返回多条数据
69.4.5. SSE
69.4.6. 末尾连接字符串
69.4.7. Flux scan
69.4.8. ConnectableFlux
69.4.9. 上传文件并保存
69.5. Newline Delimited JSON (NDJSON)
69.6. WebClient
69.6.1. 配置 WebClient
69.6.2. @Controller/@RestController 实例
69.6.3. Get 请求实例
69.6.4. URI 参数
69.6.5. 查询参数
69.6.6. Post 操作演示
69.6.7. Post 表单数据
69.6.8. 上传文件
69.6.9. 设置 HTTP 头
69.6.10. 同步阻塞等待返回结果
69.6.11. websocket
69.6.12. 获取 HTTP 链接状态
69.6.13. Http Base Authentication - 401 Unauthorized
69.6.14. SSE(Server-Sent Events)
69.6.15. 超时时间
69.6.16. share() 共享订阅数据
69.6.17. 打印调试日志
69.6.18. 解决 WebClient 截断 URI的问题
69.6.19. 异常处理
69.7. Webflux 安全
69.7.1. Token 拦截器
69.7.2. JWT
69.7.3. spring-boot-starter-security
69.8. R2DBC
69.8.1. MySQL
69.8.2. R2dbcEntityTemplate
69.8.3. MySQL Point 数据类型
69.9. Webflux Redis
69.9.1. Maven Redis 依赖
69.9.2. Redis 配置
69.9.3. Config
69.9.4. Service
69.9.5. RestController
69.10. Webflux Mongdb
69.10.1. Maven 依赖
69.10.2. Repository
69.10.3. Service
69.10.4. 控制器
69.11. 常见问题
69.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.
69.11.2. @EnableWebFluxSecurity 与 @EnableReactiveMethodSecurity 不生效
69.11.3. webflux netty 不支持 Content-Type: application/x-www-form-urlencoded

69.1. Getting Started

69.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>
    		
    		
			

69.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);
	}

}
    		
    		
			

69.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!");
	}

}
    		
    		
			

69.1.4. 测试

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