2V0-72-22 practice question 5 of 15

Which two statements are true about @Controller annotated classes? (Choose two.)

Full set: 79 questions
Answer options
A:

A. The @Controller annotated classes can only render views.

B:

B. The classes are eligible for handling requests in Spring MVC.

Correct answer
C:

C. The classes must be annotated together with @EnableMvcMappings to be discovered via component scanning.

D:

D. @Controller is interchangeable with @RestController with no extra code changes for the methods inside the class.

E:

E. The @Controller annotation is a stereotype annotation like @Component.

Correct answer
Correct Answer:
B. B. The classes are eligible for handling requests in Spring MVC.; E. E. The @Controller annotation is a stereotype annotation like @Component.
Explanation

The @Controller annotation is a specialization of the @Component annotation, making it a stereotype. This means that any class annotated with @Controller is eligible for component scanning and will be registered as a bean in the application context. Its primary role in Spring MVC is to mark a class as a web controller, making its methods eligible to handle incoming web requests when mapped with annotations like @RequestMapping.

Why Incorrect

A. @Controller classes can return data directly (e.g., JSON) by using the @ResponseBody annotation on a method, not just render views.
C. @EnableMvcMappings is not a standard Spring annotation. Component scanning, enabled by @ComponentScan or @SpringBootApplication, discovers @Controller beans.
D. @RestController is a composite annotation that includes @Controller and @ResponseBody. They are not interchangeable without code changes, as methods in a @Controller do not implicitly have @ResponseBody behavior.

References

1. Spring Framework Documentation, 6.1.6. "Web on Servlet Stack", Section 22.3.1. "Defining a Controller with @Controller". States that @Controller indicates that a class serves the role of a controller and is a stereotype for component scanning.

2. Javadoc for org.springframework.stereotype.Controller. The annotation's source code shows it is meta-annotated with @Component.

3. Spring Framework Documentation, 6.1.6. "Web on Servlet Stack", Section 22.3.4. "@RestController". Explains that @RestController is a convenience annotation that combines @Controller and @ResponseBody.

Scroll to Top