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

第 43 章 WebFlux framework

目录

43.1. Getting Started
43.1.1. Maven
43.1.2. Application
43.1.3. RestController
43.1.4. 测试
43.2. WebFlux 与 SprintMVC 有什么不同?
43.2.1. 实验程序
43.2.2. 实验结果
43.3. WebFlux Router
43.3.1. Component 原件
43.3.2. 路由配置
43.4. RestController
43.4.1. Post 数据
43.4.2. Thymeleaf
43.4.3. Mono
43.4.4. Flux 返回多条数据
43.4.5. SSE
43.4.6. 末尾连接字符串
43.4.7. Flux scan
43.4.8. ConnectableFlux
43.4.9. 上传文件并保存
43.5. Newline Delimited JSON (NDJSON)
43.6. WebClient
43.6.1. 配置 WebClient
43.6.2. @Controller/@RestController 实例
43.6.3. Get 请求实例
43.6.4. URI 参数
43.6.5. 查询参数
43.6.6. Post 操作演示
43.6.7. Post 表单数据
43.6.8. 上传文件
43.6.9. 设置 HTTP 头
43.6.10. 同步阻塞等待返回结果
43.6.11. websocket
43.6.12. 获取 HTTP 链接状态
43.6.13. Http Base Authentication - 401 Unauthorized
43.6.14. SSE(Server-Sent Events)
43.6.15. 超时时间
43.6.16. share() 共享订阅数据
43.6.17. 打印调试日志
43.6.18. 解决 WebClient 截断 URI的问题
43.6.19. 异常处理
43.7. Webflux 安全
43.7.1. Token 拦截器
43.7.2. JWT
43.7.3. spring-boot-starter-security
43.8. R2DBC
43.8.1. MySQL
43.8.2. R2dbcEntityTemplate
43.8.3. MySQL Point 数据类型
43.9. Webflux Redis
43.9.1. Maven Redis 依赖
43.9.2. Redis 配置
43.9.3. Config
43.9.4. Service
43.9.5. RestController
43.10. Webflux Mongdb
43.10.1. Maven 依赖
43.10.2. Repository
43.10.3. Service
43.10.4. 控制器
43.11. 常见问题
43.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.
43.11.2. @EnableWebFluxSecurity 与 @EnableReactiveMethodSecurity 不生效
43.11.3. webflux netty 不支持 Content-Type: application/x-www-form-urlencoded

43.1. Getting Started

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

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

}
    		
    		
			

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

}
    		
    		
			

43.1.4. 测试

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