Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can anybody explain me difference between class level controller and method level controller..?

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 ???

like image 837
JOHND Avatar asked May 03 '12 05:05

JOHND


People also ask

What is @RequestMapping annotation used for?

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.

What is @RequestMapping in spring boot?

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.

Why do we need controller class in Java?

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.

Which type of class is controller in spring?

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.


1 Answers

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
  • 16.3 Implementing Controllers
like image 155
Sean Patrick Floyd Avatar answered Sep 29 '22 20:09

Sean Patrick Floyd