Search⌘ K
AI Features

Wrapping Things Inside a Service

Learn how to improve Spring Boot application design by moving the addToCart functionality from controllers to dedicated services. Discover how using services keeps controllers focused on handling web requests while business logic is managed separately. Explore method references and dependency injection to simplify and refactor code, resulting in a cleaner, more maintainable reactive e-commerce application.

Right now, the addToCart functionality is part of the controller, which doesn’t reflect good design.

This is why the code for addToCart() is so dense, because we don’t have all those temporary variables and intermediate bits of state. By cutting that out, what’s left are operations that do the real lifting. Each line moves things forward. Hence, we’ll simply move it to a different service.

A consequence of this is that our web controllers can quickly get too heavy. It’s recommended that we keep controllers ...