Springboot
Springboot
@RestController vs @Controller
In Spring MVC, both @RestController and @Controller are used to define controller classes, but
1. Purpose:
● @Controller: It is typically used for traditional web applications that serve HTML
@Controller
@RequestMapping("/hello")
@GetMapping("/greeting")
modelAndView.setViewName("greeting");
return modelAndView;
Example
@RestController
@RequestMapping("/api")
@GetMapping("/greeting")
return ResponseEntity.ok(message);
2. Response Handling:
response directly as JSON or XML, eliminating the need for explicit view resolution.
The return type of the methods is serialized and sent as the response body.
annotation. It allows the component scanning mechanism to detect and register the
that is primarily used for RESTful APIs. The @ResponseBody annotation is not
4. Return Types:
● @Controller: Controller methods can return various types such as String (view
as the response body. They commonly use return types like String, List, Map, or
custom objects. Spring automatically serializes the return value to JSON or XML
In summary, the choice between @Controller and @RestController depends on the purpose of
your application. If you're building a traditional web application with server-side rendering, use
@Controller. For building RESTful APIs that primarily deal with JSON or XML responses, use
deserialization.