一、在 pom.xml 文件中引入 spring-boot-starter-webflux 依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
二、使用
1、常规用法
package com.zhk.code.test;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
@RestController
public class TestController {
@SneakyThrows
@RequestMapping("/")
public Mono<String> test() {
return Mono.just("xxxxtest");
}
}
2、路由写法
package com.zhk.code.test;
import org.springframework.stereotype.Controller;
import reactor.core.publisher.Mono;
@Controller
public class RouterController {
public Mono<String> test() {
return Mono.just("success");
}
}
package com.zhk.code.test;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.*;
@Configuration
public class RouterConfig {
@Bean
public RouterFunction<ServerResponse> getString(RouterController routerController) {
return RouterFunctions
.route(RequestPredicates.GET("/hello"),
request -> ServerResponse.ok().body(routerController.test(), String.class));
}
}
三、集合得写法
上述得都是实体类
@GetMapping("/list")
public Flux<Integer> list() {
System.out.println("xxxx");
List<Integer> list = Arrays.asList(1, 2, 3);
return Flux.fromIterable(list);
}