Difference between @Controller and @RestController in Spring Boot and Spring MVC?

@Controller is used to declare common web controllers which can return HTTP response but @RestController is used to create controllers for REST APIs which can return JSON.

Soma
Javarevisited

--

Difference between @Controller and @RestController in Spring Boot and Spring MVC?
image_credit — javarevisited

Hello folks, if you are preparing for Java and Spring Developer interview then you may have come across “difference between @Controller and @RestController annotation” in Spring Boot and Spring Framework in general. It’s one of the popular spring question which I have also mentioned in my list of 25 Spring Framework questions earlier

In Spring MVC, both @Controller and @RestController annotations are used to define web controllers as per MVC Design pattern. A controller is responsible for handling HTTP request and returning HTTP response to client.

The main difference between these two annotation is how they handle client’s request and when they are used

For example @Controller annotation is there from day 1 and it is used to mark a class as a web controller to process HTTP requests and return a view name, which is then resolved by a view resolver to generate the final HTML view.

--

--

Soma
Javarevisited

Java and React developer, Join Medium (my favorite Java subscription) using my link 👉 https://medium.com/@somasharma_81597/membership