Junior — Middle
Will DispatcherServlet find a controller with the @Controller annotation when processing a request?
sobes.tech AI
Answer from AI
Yes, DispatcherServlet in Spring MVC, when processing an HTTP request, looks for controllers annotated with @Controller. DispatcherServlet acts as a front controller that receives the request, determines the appropriate controller method based on the URL and HTTP method, and then invokes that method to handle the request.
The @Controller annotation indicates that the class is a Spring MVC component that handles web requests. DispatcherServlet uses Spring mechanisms to scan and register such controllers at application startup.
Example of a controller:
@Controller
public class MyController {
@GetMapping("/hello")
public String sayHello() {
return "hello"; // view name
}
}
When a request is made to /hello, DispatcherServlet will find the sayHello method and invoke it.