Junior — Middle
Can you explain what HandlerMapping is and how it is used in web frameworks?
sobes.tech AI
Answer from AI
HandlerMapping is a component in Java web frameworks (such as Spring MVC) responsible for mapping HTTP requests to specific handlers (controllers).
When a request comes in, HandlerMapping analyzes the URL, HTTP method, and other parameters to determine which controller method should handle the request.
In Spring MVC, for example, HandlerMapping uses annotations like @RequestMapping or other mechanisms to match the request path with controller methods.
Example:
@Controller
public class MyController {
@RequestMapping("/hello")
public String sayHello() {
return "helloView";
}
}
Here, HandlerMapping will associate the /hello request with the sayHello method.
Thus, HandlerMapping is a key component in routing requests within a web application, ensuring proper distribution of requests to handlers.