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

第 68 章 WebFlux framework

目录

68.1. Getting Started
68.1.1. Maven
68.1.2. Application
68.1.3. RestController
68.1.4. 测试
68.2. WebFlux 与 SprintMVC 有什么不同?
68.2.1. 实验程序
68.2.2. 实验结果
68.3. WebFlux Router
68.3.1. Component 原件
68.3.2. 路由配置
68.4. RestController
68.4.1. Post 数据
68.4.2. Thymeleaf
68.4.3. Mono
68.4.4. Flux 返回多条数据
68.4.5. SSE
68.4.6. 末尾连接字符串
68.4.7. Flux scan
68.4.8. ConnectableFlux
68.4.9. 上传文件并保存
68.5. Newline Delimited JSON (NDJSON)
68.6. WebClient
68.6.1. 配置 WebClient
68.6.2. @Controller/@RestController 实例
68.6.3. Get 请求实例
68.6.4. URI 参数
68.6.5. 查询参数
68.6.6. Post 操作演示
68.6.7. Post 表单数据
68.6.8. 上传文件
68.6.9. 设置 HTTP 头
68.6.10. 同步阻塞等待返回结果
68.6.11. websocket
68.6.12. 获取 HTTP 链接状态
68.6.13. Http Base Authentication - 401 Unauthorized
68.6.14. SSE(Server-Sent Events)
68.6.15. 超时时间
68.6.16. share() 共享订阅数据
68.6.17. 打印调试日志
68.6.18. 解决 WebClient 截断 URI的问题
68.6.19. 异常处理
68.7. Webflux 安全
68.7.1. Token 拦截器
68.7.2. JWT
68.7.3. spring-boot-starter-security
68.8. R2DBC
68.8.1. MySQL
68.8.2. R2dbcEntityTemplate
68.8.3. MySQL Point 数据类型
68.9. Webflux Redis
68.9.1. Maven Redis 依赖
68.9.2. Redis 配置
68.9.3. Config
68.9.4. Service
68.9.5. RestController
68.10. Webflux Mongdb
68.10.1. Maven 依赖
68.10.2. Repository
68.10.3. Service
68.10.4. 控制器
68.11. 常见问题
68.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.
68.11.2. @EnableWebFluxSecurity 与 @EnableReactiveMethodSecurity 不生效
68.11.3. webflux netty 不支持 Content-Type: application/x-www-form-urlencoded

68.1. Getting Started

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

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

}
    		
    		
			

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

}
    		
    		
			

68.1.4. 测试

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