Benefits of having HTTP endpoints return Flux/Mono instances instead of DTOs [closed]

天大地大妈咪最大 提交于 2019-12-03 03:43:52

问题


I've watched Spring Tips: Functional Reactive Endpoints with Spring Framework 5.0 and read a little about spring reactor but I can't quite understand it.

What are the benefits of having endpoints return Flux/Mono instances (jacksonified) instead of straight up dto objects (jacksonified), given that I've got netty and spring reactor active? I initially assumed that reactive streams would, in http request/response context, work more like websockets wherein the server pushes the data to the receiver with an open channel but this doesn't seem to be the case.

Also what does netty actually do better in reactive programming than tomcat?

I'm sorry if these questions seem stupid but I don't quite understand the purpose of this new framework direction. Why did it come about, how does it work and what problems does it solve?


回答1:


I highly suggest you watch the recently presented in Devoxx Belgium "Reactive Web Application with Spring 5" by Rossen Stoyanchev.

In there he talks about how the Reactive Web Controller (presented below) on the surface looks like Spring MVC HTTP Servlet Request/Response Controller but it's actually not

@GetMapping("/users/{id}")
public Mono<User> getUser(@PathValiable Long id) {
   return this.userRepository.findById(id);
}

@GetMapping("/users")
public Flux<User> getUsers() {
   return this.userRepository.findAll();
}

he talks about how Servlet 3.1 although non-blocking doesn't truely work for fully reactive and how the glue code connecting the Servlet 3.1 and Reactive Streams is implemented as part of the Spring 5 changes for the Servlet 3.1 compliant web containers (Jetty and Tomcat).

And of course he is touching on fully Reactive non-blocking compliant servers (Netty, Undertow) are supported to run Reactive Streams.




回答2:


It's not right to mean that Netty is better than tomcat. The implementation is different. Tomcat uses java NIO to implement servlet 3.1 spec. Meantime, netty uses NIO as well but introduces custom api. If you want to get insight in how does servlet 3.1 implemeted in Netty, watch this video https://youtu.be/uGXsnB2S_vc



来源:https://stackoverflow.com/questions/40964700/benefits-of-having-http-endpoints-return-flux-mono-instances-instead-of-dtos

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!