Icon for gfsd IntelliJ IDEA

Spring web framework comparison: Spring Web MVC vs Spring WebFlux

Spring MVC or Spring WebFlux? Read our comparison

In this post, we dive into the many differences between Spring Web MVC and Spring WebFlux to help you determine which one to use for your project.

Users new to the Spring framework can get overwhelmed by the many related terms and components of Spring. If you’re looking to create RESTful APIs in Spring, you may have come across Spring Web MVC and Spring WebFlux. But which one should you use for your specific project? This article will clarify the basics of and differences between Spring MVC vs Spring WebFlux.

🌱 How to test Spring Boot applications?

Check out our guide to testing Spring Boot applications with Symflower, a powerful one-click test generation tool: Best practices for testing Spring Boot applications: a complete guide.

Need a guide to mocking? Find out How to do mocking in Spring Boot?

What is Spring Web MVC?

Spring Web MVC (or, as most people know it, Spring MVC) is a web framework within the Spring framework used by almost a third of Java developers. Spring MVC is the original Spring web framework built on the Servlet API and it has been part of Spring from the get-go.

Spring MVC handles incoming HTTP requests to the appropriate handlers. Its name refers to Spring’s “Model-View-Controller” architectural design pattern which uses these three main components to build web applications:

  • Model: Encapsulates the application data. Used to set the rules, logic, and data structures used by the application.
  • View: Used to specify UI logic. Renders the model data and generates an HTML output for the user.
  • Controller: Used to process (interpret and validate) user input. Passes data to the View component for rendering to the user.

Spring Web MVC is generally used for developing and deploying complex web applications. A key advantage of Spring MVC is that it enables fast deployment, especially in an environment where multiple developers work in parallel.

It promotes reusability through the simple (re)use of existing business objects. Spring MVC is also highly customizable, making it a flexible choice for a variety of applications. In addition, it makes debugging easier since applications based on Spring Web MVC have multiple levels. Spring Web MVC supports Tomcat, Jetty, and Undertow servers.

Spring’s Model-View-Controller pattern
Image source: https://docs.spring.io/spring-framework/docs/2.5.0/reference/mvc.html

What is Spring WebFlux?

WebFlux is a reactive-stack web framework that was added to Spring to enable concurrency handling and scalability. It’s a non-blocking web stack that allows you to use multiple concurrent threads. Non-blocking means that in the event of making a blocking call, the thread will be freed up for other requests.

WebFlux is dependent on the Reactor library which focuses on server-side Java, but you can also use other reactive libraries (like RxJava) via Reactive Streams. Spring WebFlux offers functional web endpoints in combination with annotated controllers. WebFlux supports a variety of servers like Netty, Tomcat, Jetty, Undertow, and Servlet containers, but it doesn’t have built-in support for starting or stopping servers.

🤓 New to Spring Boot?

Check out Symflower’s Introduction to Spring Boot. Feel like you need a better grasp on the Spring framework? Read our comparison of Spring vs Spring Boot vs Spring Web MVC vs Spring WebFlux

Differences & use cases of Spring Web MVC vs Spring WebFlux

Spring MVC and Spring WebFlux co-exist in the Spring Framework, leaving it up to you to decide which one to use. Some applications combine both modules, as they are designed for continuity and consistency.

Spring recommends that if you already have a working Spring MVC application, you don’t change the framework. However, if you’re just starting a new project, here are some of the key differences and considerations:

  • For most use cases, Spring Web MVC should be fine.
  • The main difference between the two frameworks is that requests to Spring MVC use a single thread whereas Spring WebFlux is fully non-blocking e.g. if the application makes a blocking call, the thread will not wait and can be used for processing other requests.
  • If you predict that your application will need to scale with high loads, consider using WebFlux.
  • It’s a good idea to start by checking the dependencies of your project. For applications that use blocking persistence APIs or networking APIs, WebMVC is a sound choice.
  • For smaller applications where there is a need for transparency and control but requirements aren’t as complex, Spring WebFlux functional web endpoints are a good option.
  • The same stands for microservices, but in this case, you may also decide to combine Spring Web MVC and Spring WebFlux controllers.
  • Bear in mind that switching to WebFlux can be a challenge for developers because making the transition to non-blocking, functional, declarative programming requires some learning and getting used to.
Spring MVC or Spring WebFlux?
Image source: https://docs.spring.io/spring-framework/reference/web/webflux/new-framework.html

Summary: the basics of Spring web frameworks MVC and WebFlux

That should have given you a decent understanding of the basics of Spring Web MVC and Spring WebFlux. Use the above information to decide which one to use for your projects.

Whichever you end up choosing, don’t forget to adequately test your application. Check out our guide: Best practices for testing Spring Boot applications!

Try Symflower in your IDE to generate unit test templates & test suites

Make sure you never miss any of our upcoming content by signing up for our newsletter and by following us on Twitter, LinkedIn or Facebook!

| 2024-03-11