preface
Reactive Web Flux is a major feature of Spring since release 5.0 for Java developers. Is this a replacement for Spring Web MVC? Or is it just finally possible to stop being based on Servlet containers?
Web MVC based on Servlet container
As a Java developer, I’m no stranger to the Spring framework. It began with the Interface 21 framework in Rod Johnson’s 2002 book Expert One-On-One J2EE Design and Development, and in 2004, with Spring 1.0, From XML to 3.0, JavaConfig Settings are supported; Furthermore, in 2014, in addition to Spring 4.0, Spring Boot was released for the first time. The biggest highlight was the adoption of automatic configuration, which made rapid spring-based development possible.
For Web developers, the Web MVC framework in Spring, which has grown with Spring, was criticized early on for being difficult to test due to its Servlet container base (e.g., the controller included a Servlet API).
However, from the hands-on Controller interface with XML Settings to the annotations with JavaConfig, Web MVC has become more and more convenient to use. If desired, a gradual approach can be adopted to gradually refactor Web applications based on Servlet API to almost no Servlet API, so as to achieve the effect of shielding the Servlet API at the program code level.
Since many Java developers have accumulated their Web development experience from Servlet containers, a Web MVC framework based on Servlet APIS is an advantage at this point. Because while it’s possible to write with Web MVC without directly facing the Servlet API, it also means being more constrained by Spring, sometimes not being able to find a solution in the Settings or API, sometimes because the mental model is still hanging in the Servlet container, HttpSession: ServletContext: HttpSession: ServletContext
It’s still true that Web MVC is based on a Servlet container, even if you don’t use the Servlet API when you write your program, because you still have to rely on Servlet container features at the bottom, like Spring Security, Essentially, the Filter scheme is based on the Servlet container.
Today, however, servlets are seen by many developers as a symbol of an old, outdated technology. Perhaps this is why, during the announcement of Java EE 8, when Servlet 4.0 was mentioned on some occasions, I always hear suggestions like “Web Flux can now be decouped from servlets.”
Implement a Reactive Streams Reactor
It is true that Web Flux does not rely on Servlet containers, however, before we can talk about Web Flux, we must first know about the Reactor project, which was launched by Pivotal, the current owner of Spring, The Reactive Streams specification is implemented to support the real-world work of Reactive Programming.
Since you are implementing the Reactive Streams specification, developers must think of RxJava/RxJava 2, or Java 9’s Flow API. This also means that developers must have an understanding of the Reactive Programming paradigm before they can use Web Flux.
Why isn’t Spring directly based on RxJava 2, instead of building a Reactive Streams project?
Technically, Reactor built on Java 8 and fully embraced the new apis that came after Java 8, such as lambda-related interfaces, new date and time apis, which meant that if the project was still based on Java 7 or earlier, You can’t use Reactor.
At the API level, RxJava 2 has to reserve some types or operations that are easy to be confused or confused due to the historical development, and Reactor has a specific API to replace them. However, It also provides conversions to RxJava 2 (and even the Flow API).
Reactor, on the other hand, is intuitive and easy to use. For example, Mono and Flux, which are most commonly introduced, implement Reactive Streams’ Publisher interface and simplify information publishing, allowing developers to You don’t have to deal with Subscriber and Subscription details (which are also implemented in Reactor, of course). In Spring Web Flux, Mono and Flux are also the main operation objects. To learn how to use Mono and Flux, see Reactor for Reaction Programming.
Another Web framework?
In Spring 5, WebFlux is added as a Reactive Web solution based on Reactor. We have introduced many simple examples of files, such as “Developing Reactive Web Applications using Spring 5’s WebFlux”. You can see Flux, Mono used to demonstrate, and the code looks like Spring MVC.
This is because Web Flux provides a way based on Java annotations. Many annotations used in Web MVC are also used in Web Flux, making it easy for developers familiar with Web MVC to understand and get started with Web Flux. However, Is this just the new Web framework?
Actually, of course not. Web Flux does not depend on Web MVC and is based on a Reactor, which is essentially a mental model of asynchronous, non-blocking and Reactive Programming. Therefore, if you want to run Web Flux on a Servlet container, Servlet 3.1 and above must be supported because there is non-blocking input/output support. Although the Web Flux API does provide blocking options in some places, there is no benefit in simply trying to adapt a Web MVC-based application to apply Web Flux. Instead, they struggle to figure out how to implement the corresponding solution in Web Flux.
For example, Spring Security is obviously not available at this point because it is Spring’s servlet-based Security solution and developers must find a way to leverage Spring Security Reactive; In addition, Spring Data Reactive is not directly used in the storage scheme.
Even if the relevant Settings and apis are applied, to reap the benefits of Web Flux, the relevant components in the application must be thoroughly reviewed and redesigned to non-blocking, Reactive Programming-based approaches, which is probably the most difficult and troublesome part.
In addition to a Java annotation-based approach that is easy for developers familiar with Web MVC to understand, Web Flux also provides a functional design and configuration approach.
In fact, Web Flux is no exception to the fact that we must be familiar with functional thinking in order to fully master the practical operations of Reactive Streams such as RxJava 2/Reacto R.
Can you leave the Servlet container?
The Servlet container is a symbol of the past, and many developers would be happy to be able to mask the Servlet container or related API. There is one less layer of abstraction and no need to use a fat Servlet container, which is certainly an advantage that comes with using Web Flux. However, If only to mask servlets, there are already other technical options.
Web MVC, which is based on servlets all the way up, can now plug in some functional designs here and there, but what remains essentially the same is that implicit in the technology stack is still a synchronous, blocking, imperative mental model. If in such a stack developers are always frustrated by the need to be asynchronous, non-blocking, Reactive, functional, Web Flux may be a viable option, not just as an alternative to Web MVC out of the Servlet container.
Overall, Web Flux is still a new technology and has yet to be proven. If you are just trying to replace Web MVC with Web Flux, or even more ambitious, just want to get out of the Servlet container, it’s best to take a thorough look before taking any action. Make sure that you or your team members are ready to accept the mental model of Web Flux, or that there are actual application scenarios.
From: OSChina, author: Data Xinghe
Links:My.oschina.net/u/3953752/b…