DEV Community

Cover image for ๐Ÿš€ Building Spring by Example: Completing the Spring Web (Spring MVC) Module
Mujuzi Moses ๏ฃฟ
Mujuzi Moses ๏ฃฟ

Posted on

๐Ÿš€ Building Spring by Example: Completing the Spring Web (Spring MVC) Module

When I started learning Spring, I noticed something about many of the tutorials I found.

They often showed how to use a feature, but not always why it worked or when you should choose one approach over another.

I learn best by building things and documenting what I discover, so I started a project called spring-by-example.

The idea is simple: instead of building one large application, Iโ€™m creating a collection of small, focused examples where each example explores a specific Spring concept with clear explanations, tests, and documentation.

Iโ€™m happy to say that Iโ€™ve now completed Module 10 โ€” Spring Web (Spring MVC). ๐ŸŽ‰

The module covers:

  • DispatcherServlet
  • Controllers
  • Request Mapping
  • Path Variables
  • Request Parameters
  • Request Body
  • Response Body
  • Model and View
  • View Resolvers
  • Exception Handling
  • Request Scope
  • Session Scope
  • Application Scope

One of the things I enjoyed about this module was understanding what actually happens when an HTTP request enters a Spring MVC application.

The DispatcherServlet acts as the front controller, receiving incoming requests and coordinating the request-handling process. Controllers then define how those requests should be handled using mappings such as @GetMapping and @PostMapping.

I also explored different ways of working with request data, including path variables, request parameters, and request bodies, as well as how Spring converts incoming data into Java objects.

The view-related examples helped connect the pieces between a controller, a model, and a view. ModelAndView can be used to return both model data and a logical view name, while ViewResolver determines which actual view should be rendered.

Exception handling was another important part of the module. Using mechanisms such as @ExceptionHandler and @ControllerAdvice, it becomes possible to keep error-handling logic organized instead of putting it directly into every controller.

Finally, I explored Spring's web scopes - request, session, and application scope โ€” and how the lifetime of a bean can be tied to the lifecycle of an HTTP request, user session, or web application.

The goal isnโ€™t just to collect Spring annotations and APIs. Iโ€™m trying to understand how Spring works under the hood, how its features fit together, and why these features exist, one concept at a time.

The project is open source, and Iโ€™m continuing to build it as I learn:

๐Ÿ”— GitHub: https://gith.ub/dsAQrAU5

๐Ÿ”— GitLab: https://gitl.ab/gfDEyGH3

Thereโ€™s still a long way to go, but completing another module feels like a good milestone. ๐Ÿš€

Next up: Module 11 - Testing - exploring how to test Spring applications, starting with unit testing and the Spring TestContext, followed by @SpringBootTest, @MockBean, @TestConfiguration, and integration testing.

If youโ€™ve worked with Spring MVC before, whatโ€™s one part of the request-handling process you think developers commonly misunderstand?

java #springframework #opensource #github #gitlab #learninginpublic #backend #softwareengineering #spring #springmvc

Top comments (0)