Sobes.tech
Junior — Middle

What parameters or settings should a controller have to be detected by DispatcherServlet?

sobes.tech AI

Answer from AI

For the DispatcherServlet to detect a controller in Spring MVC, the controller must be:

  • Annotated with @Controller or @RestController.
  • Located in a package scanned by Spring's component scanner (usually the packages specified in @ComponentScan).
  • Have methods annotated with @RequestMapping or its specialized variants (@GetMapping, @PostMapping, etc.) to bind HTTP requests to methods.

Example:

@Controller
public class MyController {
    @GetMapping("/hello")
    public String sayHello() {
        return "hello"; // view name
    }
}

If these conditions are met, the DispatcherServlet will be able to detect and invoke the controller methods for the corresponding requests.