Why Controllers Shouldn't Think in Spring Boot

Day 19. I stopped putting business logic in controllers. Not because it doesn’t work. Because it turns into a mess. I had code like this: @PostMapping("/users") public ResponseEntity<User> createUser(@RequestBody User user) { if (userRepository.existsByEmail(user.getEmail())) { throw new RuntimeException("Email already exists"); } user.setCreatedAt(LocalDateTime.now()); return ResponseEntity.ok(userRepository.save(user)); } Looks fine. Until your project grows. Now your controller is: → validating business rules → talking to the database → handling the request That’s not a controller. That’s 3 responsibilities in one place. And it only gets worse with time. That’s when it clicked. Controllers shouldn’t think. They should delegate. So I changed it. (see implementation below 👇) What I gained: → Clarity — each layer has one job → Testability — logic tested without HTTP → Reusability — service can be used anywhere The hard truth: → Fat controllers work in tutorials → They fail in real systems → Senior devs spot this instantly Writing endpoints is easy. Keeping your layers clean is what makes you a backend developer. Are your controllers doing too much? 👇 Drop it below #SpringBoot #Java #BackendDevelopment #CleanCode #JavaDeveloper

  • text

Hot take: If your controller has if/else logic or directly calls the repository — It’s already doing too much. Controllers should delegate. Services should decide. Is your controller clean or overloaded? 👇

Like
Reply

To view or add a comment, sign in

Explore content categories