Mono<ServerResponse> 与 Mono<ResponseEntity<MyPojo>> 作为 Java Spring Webflux @RequestMapping 中的返回类型

Pat*_*Pat 6 java spring-webflux

关于使用Mono<ServerResponse>vsMono<ResponseEntity<MyPojo>>作为返回类型的小问题。

我看到的示例有时Mono<ServerResponse>被用作 Spring Webflux 功能路由中的返回类型。

但也看到有时Mono<ResponseEntity<MyPojo>>在 Spring 功能路由中用作返回类型的示例。

此外,还有很多Mono<ResponseEntity<MyPojo>>在@Controller、@RequestMapping(Get、Post 映射等...)中使用作为返回类型的示例。

但返回的例子也很少Mono<ServerResponse>

请问一下,退货有什么区别吗?

K.N*_*las 12

据我了解,它的缺点是org.springframework.http.ResponseEntity来自原始的 Spring Mvc Framework 包,并且 org.springframework.web.reactive.function.server.ServerResponse来自 spring Reactive 包。

反应式包有一个“兼容”模式,允许您使用向后兼容的@RestController注释,它们通常会ResponseEntity像以前一样返回 a 。

没有兼容性功能的反应式包实现使用RouterHandler,通常返回ServerResponse。有关 spring 的教程,请参阅构建响应式 RESTful Web 服务。

另请参阅 Spring WebFlux 指南,第1.4 章。带注释的控制器用于兼容反应式方式和1.5。功能方式的功能端点。

如果您仔细查看代码,Annotated Controllers您会发现它使用了该Functional Endpoints代码。