【问题标题】:Mono<ServerResponse> vs Mono<ResponseEntity<MyPojo>> as return type in Java Spring Webflux @RequestMappingMono<ServerResponse> 与 Mono<ResponseEntity<MyPojo>> 作为 Java Spring Webflux @RequestMapping 中的返回类型
【发布时间】:2021-03-25 10:11:32
【问题描述】:

关于使用Mono&lt;ServerResponse&gt; vs Mono&lt;ResponseEntity&lt;MyPojo&gt;&gt; 作为返回类型的小问题。

我看到了一些例子,有时,Mono&lt;ServerResponse&gt; 在 Spring Webflux 的功能路由中用作返回类型。

但也看到了一些例子,有时,Mono&lt;ResponseEntity&lt;MyPojo&gt;&gt; 被用作 Spring 功能路由中的返回类型。

此外,Mono&lt;ResponseEntity&lt;MyPojo&gt;&gt; 在@Controller 中使用的例子很多,@RequestMapping (Get, Post mapping etc...) 作为返回类型。

但也有少数例子返回Mono&lt;ServerResponse&gt;

请问,退货有什么区别?

【问题讨论】:

    标签: java spring-webflux


    【解决方案1】:

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

    响应式包具有“兼容性”模式,可让您使用向后兼容的 @RestController 注释,它们通常会像以前一样返回 ResponseEntity

    没有兼容性功能的响应式包实现使用RouterHandler,通常返回ServerResponse。有关 spring 的教程,请参阅 Building a Reactive RESTful Web Service

    另请参阅 Spring WebFlux 指南,章节 1.4. Annotated Controllers 了解兼容性反应方式,1.5. Functional Endpoints 了解功能方式。

    如果您深入了解Annotated Controllers 代码,您会发现它使用了Functional Endpoints 代码。

    【讨论】:

      猜你喜欢
      • 2021-11-03
      • 2020-01-06
      • 2021-09-09
      • 2018-09-21
      • 1970-01-01
      • 2019-05-18
      • 2021-05-08
      • 2021-11-09
      • 1970-01-01
      相关资源
      最近更新 更多