Omkar Dumpa’s Post

What Actually Happens When We Use @RestController in Spring Boot? When I first saw the annotation @RestController, I honestly didn’t know why it was used. Everyone was using it ,So I started using too. But one day I paused and asked myself: What is Spring actually doing when I write @RestController? That’s when things slowly started making sense. In Spring , there are two common annotations we used with Controllers: 1.@Controller 2.@RestController At first, they look almost the same. But they behave very differently. ->@Controller When we use @Controller , Spring assumes we are returning a view. That means : It expects an HTML page. This is mostly used in traditional Spring MVC applications where the backend returns web pages. -> @RestController When we use @RestController, we are telling Spring: i . Don’t return a view. ii . Return the data directly in the response body. Internally, @RestController is simply a combination of: @Controller + @ResponseBody So instead of rendering HTML , Spring sends JSON directly to the client. That’s why in REST APIs, we use @RestController. Now when I use @RestController, I understand what it’s telling Spring to do. Understanding little things like this , changing how I see backend development. More tomorrow 🚀 #SpringBoot #Java #BackendDevelopment #LearningInPublic #SoftwareEngineering

  • graphical user interface, application

To view or add a comment, sign in

Explore content categories