You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring MVC中@Controller与@RequestMapping的区别及请求映射原理咨询

Hey there! Let's break down your Spring MVC questions clearly and simply:

1. What's the difference between @Controller and @RequestMapping in Spring MVC?

These two annotations play totally distinct roles in handling requests:

  • @Controller: This is a class-level annotation that marks a Java class as a Spring MVC controller. Think of it as a badge you pin on a class to tell Spring: "Hey, this class is responsible for handling HTTP requests—add it to your managed beans!" Without this annotation, Spring won't recognize the class as a request handler at all.
  • @RequestMapping: This annotation works on both classes and methods. Its core job is to map specific HTTP requests to the right handler method. For example, if you add @RequestMapping("/user") to a controller class and @RequestMapping("/profile") to a method inside it, the full request path that triggers this method will be /user/profile. It's like assigning a precise "address label" to each request, so the DispatcherServlet knows exactly which method to call for incoming requests.
  • To sum up: @Controller is the "identity card" for a request-handling class, while @RequestMapping is the "address guide" that directs requests to the correct method within that class.
2. How does DispatcherServlet map to sayHelloMethod?

Let's walk through the step-by-step process when you access localhostXXXXX\root\hello.htm:

  1. Request Reception: The DispatcherServlet first catches your HTTP request. It then cleans up the request URI—removing the context path (root) and the file extension (.htm), leaving it with the core path /hello.
  2. Lookup HandlerMapping: Spring MVC has a component called HandlerMapping that pre-collects all mapping info from classes annotated with @Controller and their @RequestMapping methods. It builds a "request path → handler method" lookup table when the application starts.
  3. Path Matching: The DispatcherServlet takes the cleaned-up /hello path and searches the lookup table. It finds the exact match with your sayHelloMethod because the method has @RequestMapping("/hello").
  4. Invoke the Method: Once it locates the right method, the DispatcherServlet calls sayHelloMethod, processes the returned ModelAndView, and sends the final response back to your browser.

About your observation: Why do both class name and @RequestMapping need to contain "hel"?

This is almost certainly due to your project's component scanning configuration. For example, if your Spring setup uses <context:component-scan> (XML) or @ComponentScan (Java config) with a filter that only registers classes whose names contain "hel", then only those @Controller classes will be recognized by Spring. At the same time, the @RequestMapping path has to match the incoming request URI (/hello) for the method to be triggered. So it seems like both need to have "hel", but really it's two separate conditions:

  • The controller class must pass the component scan filter (hence the class name rule)
  • The method's @RequestMapping must match the request path

Only when both are satisfied will the request be mapped correctly.

内容的提问来源于stack exchange,提问作者Rishi Prakash

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:17:51