2V0-72.22PSE · Question #55
Which two statements are true about @Controller annotated classes? (Choose two.)
The correct answer is B. The classes are eligible for handling requests in Spring MVC. E. The @Controller annotation is a stereotype annotation like @Component. @Controller is a stereotype annotation (like @Component, @Service, @Repository), meaning Spring's component scanning automatically detects and registers it as a bean - that's why E is correct. Being a registered bean, the class becomes eligible to handle HTTP requests in the…
Question
Which two statements are true about @Controller annotated classes? (Choose two.)
Options
- AThe @Controller annotated classes can only render views.
- BThe classes are eligible for handling requests in Spring MVC.
- CThe classes must be annotated together with @EnableMvcMappings to be discovered via
- D@Controller is interchangeable with @RestController with no extra code changes for the
- EThe @Controller annotation is a stereotype annotation like @Component.
How the community answered
(59 responses)- A2% (1)
- B93% (55)
- C3% (2)
- D2% (1)
Explanation
@Controller is a stereotype annotation (like @Component, @Service, @Repository), meaning Spring's component scanning automatically detects and registers it as a bean - that's why E is correct. Being a registered bean, the class becomes eligible to handle HTTP requests in the Spring MVC dispatcher framework, making B correct.
Why the distractors are wrong:
- A is false -
@Controllercan also return response body data (e.g., JSON) when methods are annotated with@ResponseBody; it's not limited to views. - C is false - no
@EnableMvcMappingsannotation exists;@Controllerclasses are discovered through standard component scanning (@ComponentScanor@SpringBootApplication). - D is false -
@RestController=@Controller+@ResponseBodyapplied to every method, so switching between them does require code changes (adding or removing@ResponseBodyon methods).
Memory tip: Think of @Controller as a specialised @Component - it inherits all the auto-detection benefits of @Component (stereotype), but adds Spring MVC's request-handling awareness. The key difference from @RestController is that @Controller alone won't serialize return values to the response body without @ResponseBody.
Topics
Community Discussion
No community discussion yet for this question.