简体   繁体   中英

Mono<ServerResponse> vs Mono<ResponseEntity<MyPojo>> as return type in Java Spring Webflux @RequestMapping

Small question regarding using Mono<ServerResponse> vs Mono<ResponseEntity<MyPojo>> as return type please.

I am seeing examples where sometimes, Mono<ServerResponse> is used as return type in a functional route from Spring Webflux.

But also seeing examples where sometimes, Mono<ResponseEntity<MyPojo>> is used as return type in a Spring functional route.

Moreover, there are many examples where Mono<ResponseEntity<MyPojo>> is used in the @Controller, @RequestMapping (Get, Post mapping etc...) as return type.

But there are also few examples with returning Mono<ServerResponse> .

May I ask, what is the difference between returning either one please?

The short of it, as I understand it, is that org.springframework.http.ResponseEntity is from the original Spring Mvc Framework package and org.springframework.web.reactive.function.server.ServerResponse is from the spring Reactive package.

The reactive package has a "compatibility" mode that lets you use the backward compatible @RestController annotations and they would generally return a ResponseEntity as before.

The Reactive package implementation without the compatibility features use a Router and Handler and generally return a ServerResponse . See Building a Reactive RESTful Web Service for a tutorial from spring.

See also the Spring WebFlux guide, chapters 1.4. Annotated Controllers for the compatibility reactive way and 1.5. Functional Endpoints for the functional way.

If you look under the covers at the Annotated Controllers code you will see it uses the Functional Endpoints code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM