Can @Component, @Repository and @Service annotations be used interchangeably in Spring or do they provide any particular functionality besides acting as a notation device?>
Good enough answers are here to explain the whats-the-difference-between-component-repository-service-annotations. I would like to share the difference between @Controller & @RestController
@Controller
vs RestController
@RestController
:@Controller
which adds
@Controller
and @ResponseBody
annotation automatically. so we do not have to add @ResponseBody
to our mapping methods. That means
@ResponseBody
is default active.@RestController
you cannot return a view (By using
Viewresolver
in Spring/Spring-Boot)@RestController
also converts the response to JSON/XML automatically
as @ResponseBody
makes the returned objects to something that could be in the body, e.g. JSON or XML
@Controller
@Controller
is used to mark classes as Spring MVC Controller. This
annotation is just a specialized version of @Component
and it
allows the controller classes to be auto-detected based on classpath
scanning.@Controller
you can return a view in Spring web MVC.More Detailed View