Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple @RequestMapping for a method in controller class in Spring MVC

Tags:

I have a method in controller class in Spring MVC.

@RequestMapping("/home")
    public void contactHomeDispatcher(){
    ...
    }

Is it possible to map another url for this method say "/contact". My question is whether it is possible to have multiple request mappings for a single method in a controller.

like image 962
Vaibhav Avatar asked Dec 14 '13 17:12

Vaibhav


People also ask

Can 2 controllers have same request mapping?

You cannot. A URL can only be mapped to a single controller. It has to be unique.

Which of the following is a method where RequestMapping is used with multiple URI?

@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.

Which of these are valid usage of @RequestMapping annotation in Spring MVC?

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.

Which of the following methods uses RequestMapping with a class?

RequestMapping annotation is used to map web requests onto specific handler classes and/or handler methods.


1 Answers

You cannot have multiple @RequestMappings, but you can have @RequestMappings with multiple values of attributes:

@RequestMapping({ "/home", "/contact" })

As you can see, all attrbiutes of @RequestMapping are arrays, therefore they can take multiple values.

like image 177
axtavt Avatar answered Oct 21 '22 03:10

axtavt