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

第 44 章 WebFlux framework

目录

44.1. Getting Started
44.1.1. Maven
44.1.2. Application
44.1.3. RestController
44.1.4. 测试
44.2. WebFlux 与 SprintMVC 有什么不同?
44.2.1. 实验程序
44.2.2. 实验结果
44.3. WebFlux Router
44.3.1. Component 原件
44.3.2. 路由配置
44.4. RestController
44.4.1. Post 数据
44.4.2. Thymeleaf
44.4.3. Mono
44.4.4. Flux 返回多条数据
44.4.5. SSE
44.4.6. 末尾连接字符串
44.4.7. Flux scan
44.4.8. ConnectableFlux
44.4.9. 上传文件并保存
44.5. Newline Delimited JSON (NDJSON)
44.6. WebClient
44.6.1. 配置 WebClient
44.6.2. @Controller/@RestController 实例
44.6.3. Get 请求实例
44.6.4. URI 参数
44.6.5. 查询参数
44.6.6. Post 操作演示
44.6.7. Post 表单数据
44.6.8. 上传文件
44.6.9. 设置 HTTP 头
44.6.10. 同步阻塞等待返回结果
44.6.11. websocket
44.6.12. 获取 HTTP 链接状态
44.6.13. Http Base Authentication - 401 Unauthorized
44.6.14. SSE(Server-Sent Events)
44.6.15. 超时时间
44.6.16. share() 共享订阅数据
44.6.17. 打印调试日志
44.6.18. 解决 WebClient 截断 URI的问题
44.6.19. 异常处理
44.7. Webflux 安全
44.7.1. Token 拦截器
44.7.2. JWT
44.7.3. spring-boot-starter-security
44.8. R2DBC
44.8.1. MySQL
44.8.2. R2dbcEntityTemplate
44.8.3. MySQL Point 数据类型
44.9. Webflux Redis
44.9.1. Maven Redis 依赖
44.9.2. Redis 配置
44.9.3. Config
44.9.4. Service
44.9.5. RestController
44.10. Webflux Mongdb
44.10.1. Maven 依赖
44.10.2. Repository
44.10.3. Service
44.10.4. 控制器
44.11. 常见问题
44.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.
44.11.2. @EnableWebFluxSecurity 与 @EnableReactiveMethodSecurity 不生效
44.11.3. webflux netty 不支持 Content-Type: application/x-www-form-urlencoded

44.1. Getting Started

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

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

}
    		
    		
			

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

}
    		
    		
			

44.1.4. 测试

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