@RequestMapping is one of the most common annotation used in Spring Web applications. This annotation maps HTTP requests to handler methods of MVC and REST controllers.
In this post, you’ll see how versatile the @RequestMapping annotation is when used to map Spring MVC controller methods.
Request Mapping Basics
In Spring MVC applications, the RequestDispatcher (Front Controller Below) servlet is responsible for routing incoming HTTP requests to handler methods of controllers.
When configuring Spring MVC, you need to specify the mappings between the requests and handler methods.
To configure the mapping of web requests, you use the @RequestMapping annotation.
The @RequestMapping annotation can be applied to class-level and/or method-level in a controller.
The class-level annotation maps a specific request path or pattern onto a controller. You can then apply additional method-level annotations to make mappings more specific to handler methods.
Here is an example of the @RequestMapping annotation applied to both class and methods.
With the preceding code, requests to /home will be handled by get() while request to /home/index will be handled by index().
@RequestMapping with Multiple URIs
You can have multiple request mappings for a method. For that add one @RequestMapping annotation with a list of values.
As you can see in this code, @RequestMapping supports wildcards and ant-style paths. For the preceding code, all these URLs will be handled by indexMultipleMapping()
-
localhost:8080/home
-
localhost:8080/home/
-
localhost:8080/home/page
-
localhost:8080/home/pageabc
-
localhost:8080/home/view/
-
localhost:8080/home/view/view
- RequestMapping Annotation Spring Using therequestmapping annotation spring using requestmapping参数spring 注解requestmapping spring-mvc spring 注解modelattribute spring-web annotation 注解exceptionhandler spring-web annotation requestmapping 注解requestmapping quot requestmapping application produces 注解requestmapping第三章 注解requestmapping springmvc