Difference between @Controller and RouterFunction in Spring 5 WebFlux

让人想犯罪 __ 提交于 2020-01-02 01:37:10

问题


There are two ways to expose HTTP endpoints in spring 5 now.

  1. @Controller or @RestController by making the controller's class, e.g.
@RestController
@RequestMapping("persons")
public class PersonController { 

    @Autowired
    private PersonRepo repo;

    @GetMapping("/{id}")
    public Mono<Person> personById(@PathVariable String id){
        retrun repo.findById(id);
    }
}
  1. Route in @Configuration class by using RouterFunctions:
@Bean
public RouterFunction<ServerResponse> personRoute(PersonRepo repo) {
    return route(GET("/persons/{id}"), req -> Mono.justOrEmpty(req.pathVariable("id"))                                             
                                                 .flatMap(repo::getById)
                                                 .flatMap(p -> ok().syncBody(p))
                                                 .switchIfEmpty(notFound().build()));
}

Is there any performance difference in using anyone approach? Which one should I use when writing my application from scratch.


回答1:


  • Programming Paradigm: Imperative vs Functional

In the case with the @Controller or @RestController annotations, we agree with the annotation-based model where we use annotations for mappings (and not only) and as a result side effects (that is not allowed in the functional world) to make our API works. Such side effects could be @Valid annotation that provides inbuilt bean validation for requests' bodies or @RequestMapping with the root path for the whole controller.

On the other hand, with the router functions, we get rid of annotations that consist of any side effects in terms of API implementation and delegate it directly to the functional chain: router -> handler. Those two are perfectly suited for building the basic reactive block: a sequence of events and two protagonists, a publisher and a subscriber to those events.

  • MVC Legacy: Servlets Stack vs Netty Stack

When we are talking about @Controller I would say that we usually will think in term of synchronous Java world: Servlets, ServletContext, ServletContainerInitializer, DispatcherServlet etc. Even if we will return Mono from a controller to make our application reactive we still will play in terms of Servlet 3.0 specification that supports java.nio.* and running on the same servlets containers such as Jetty or Tomcat. Subsequently, here we will use corresponding design patterns and approaches for building web apps.

RouterFunction on the other hand was inspired by the true reactive approach that originates from async Java world - Netty and its Channel Model.

Subsequently new set of classes and their APIs for reactive environment emerged: ServerRequest, ServerResponse, WebFilter and others. As for me, they were designed by the Spring team in accordance with the previous years of maintaining the framework and understanding new web systems requirements. The name for those requirements is Reactive Manifesto.

Real Case

Recently my team faced the issue that it is impossible to integrate Swagger with RouterFucntion endpoints. It could upvote for @Controlers, but the Spring team introduced their solution - Spring REST Docs that could be easily connected to reactive WebTestClient. And I use here word 'connected' cause it follows true reactive meaning behind: instead of Swagger with its overloaded configurations and side-effect annotations, you easily could build you API docs in tests without touching you working code at all.

Final Bit

Cause of no performance impact it's likely to hear something similar to "it is absolutely based on individual preference what to use". And I agree that it's individual preference indeed among two options: moving forward or moving backwards when you let yourself to stay in the same domain for a decade. I think that reactive support for @Controller was done by the Spring team to make it possible for old projects somehow be in tune with requirements of time and have at least opportunity for the migration. If you are going to create a web application from scratch then do not hesitate and use introduced reactive stack.



来源:https://stackoverflow.com/questions/47092029/difference-between-controller-and-routerfunction-in-spring-5-webflux

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