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.
Table of contents:
- What is Spring Web MVC?
- What is Spring WebFlux?
- Differences & when to use Spring Web MVC vs Spring WebFlux
- Testing Spring Boot applications
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.
Check out all the other parts of our blog post series on Spring Boot:
- Part 1: An introduction to Spring Boot: the basics
- Part 2: Spring Boot testing best practices & guide
- Part 3: Spring framework basics: Spring vs Spring Boot vs Spring Web MVC vs Spring WebFlux
- Part 5: How to do mocking in Spring Boot? Best practices for using @MockBean vs @Mock
- Part 6: What is the difference between @Controller vs @RestController in Spring Boot?
- Part 7: Spring Boot folder structure best practices
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.
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 & when to use 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.
Testing Spring Boot applications
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. Our guide will come in handy: Best practices for testing Spring Boot applications. If you’d rather spend your time with new challenges and are looking for a way to generate tests, see how Symflower handles test template generation with 10 Spring Boot applications.
Check out the video below to see Symflower generating test templates:
Make sure you never miss any of our upcoming content by signing up for our newsletter and by following us on X, LinkedIn, or Facebook.