I am new to spring framework....while searching on google..I found few examples which has @RequestMapping annoted at the class level and few examples showing it at menthod level
When to use class level RequestMapping and menthod level RequestMapping annotation...can anybody explain me difference between class level RequestMapping and method level RequestMapping ..??
so I am little bit confused about their application at :
a) Class level
b) Method level
Also I found some @Requestmapping
with type :GET/Post,whereas some examples doesn't have type parameter.
Which approach is better ..??
Is newer versions(>Spring 2.5) don't need parameter type for request mapping ???
RequestMapping annotation is used to map web requests onto specific handler classes and/or handler methods. @RequestMapping can be applied to the controller class as well as methods.
One of the most important annotations in spring is the @RequestMapping Annotation which is used to map HTTP requests to handler methods of MVC and REST controllers. In Spring MVC applications, the DispatcherServlet (Front Controller) is responsible for routing incoming HTTP requests to handler methods of controllers.
A controller class is normally a class part of the Model View Controller (MVC) pattern. A controller basically controls the flow of the data. It controls the data flow into model object and updates the view whenever data changes.
Spring MVC @Controller This is simply a specialization of the @Component class, which allows us to auto-detect implementation classes through the classpath scanning. We typically use @Controller in combination with a @RequestMapping annotation for request handling methods.
A controller must be marked as @Controller
at the class level. The @RequestMapping
annotation can be applied at both class and method level. If it is, method annotations will be interpreted as relative URLs (relative to the class-level URL). However, method level annotations must be present, otherwise the method won't be mapped.
In annotations, parameters can be marked as optional with default values. The method parameter is such a case: it defaults to GET
, but can be explicitly set to POST
or something else.
See:
@RequestMapping
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With