WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Spring Framework · Basic · question 14 of 100

What are the differences between @Controller and @RestController annotations in Spring MVC?

📕 Buy this interview preparation book: 100 Spring Framework questions & answers — PDF + EPUB for $5

The main difference between ‘@Controller‘ and ‘@RestController‘ annotations in Spring MVC is in the way they handle the HTTP response body.

‘@Controller‘ annotation is used to create a Spring MVC controller class that is responsible for handling incoming HTTP requests. It usually handles the request and prepares the response by returning a view name, which is then resolved by a view resolver. In this way, the ‘@Controller‘ annotation is used to manage the flow of views in a Spring MVC application.

On the other hand, ‘@RestController‘ annotation is used to create a class that provides a RESTful web service. It behaves like a combination of the ‘@Controller‘ and ‘@ResponseBody‘ annotations, i.e., it handles the incoming request and returns the data directly to the client, without the need of a view resolver.

‘@RestController‘ annotations ensure that the response body is serialized directly to the HTTP response in a format such as JSON or XML, without a view. This approach is particularly useful for building APIs that communicate in a standard format like JSON or XML.

For example, consider the following code snippet:

@Controller
public class MyController {

   @GetMapping("/greeting")
   public String greeting() {
       return "Hello, world!";
   }
}

In this code snippet, the ‘@Controller‘ annotation is used to create a controller class that handles incoming GET requests to the ‘/greeting‘ URL path. The method ‘greeting()‘ prepares a view named ‘Hello, world!‘ which is resolved by a view resolver to render the final response.

On the other hand, consider the following code snippet:

@RestController
public class MyRestController {

   @GetMapping("/greeting")
   public String greeting() {
       return "Hello, world!";
   }
}

In this code snippet, the ‘@RestController‘ annotation is used to create a RESTful web service that handles incoming GET requests to the ‘/greeting‘ URL path. The method ‘greeting()‘ returns a String "Hello, world!", which is directly serialized and sent as the response body in the format requested by the client.

In summary, the use of ‘@Controller‘ or ‘@RestController‘ depends on the nature of your web application - if you are building an API, it is better to use the ‘@RestController‘ annotation, while if you are building a web application that uses views, it is better to use the ‘@Controller‘ annotation.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Spring Framework interview — then scores it.
📞 Practice Spring Framework — free 15 min
📕 Buy this interview preparation book: 100 Spring Framework questions & answers — PDF + EPUB for $5

All 100 Spring Framework questions · All topics