20

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.

Vincent Ramdhanie
  • 102,349
  • 23
  • 137
  • 192
Vaibhav
  • 3,035
  • 7
  • 24
  • 27
  • Under what condition will you need this? – Vincent Ramdhanie Dec 14 '13 at 17:06
  • Can need it in an multi tenant app or other user case when you want same functionality but different URL @vincent-ramdhanie In servlet that is why they have servlet mapping besides servlet decleration – tgkprog Sep 02 '14 at 09:25
  • Duplicate to: [Multiple Spring @RequestMapping annotations](http://stackoverflow.com/questions/2513031/multiple-spring-requestmapping-annotations) – Tariq M Nasim Jul 30 '15 at 06:32

2 Answers2

41

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.

axtavt
  • 239,438
  • 41
  • 511
  • 482
  • This is exactly what I wanted. Thanks a lot. One more request. How can I add one more mapping in below method
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    – Vaibhav Dec 14 '13 at 17:38
  • got it...no need to reply – Vaibhav Dec 14 '13 at 17:41
3

Alternative to above, you can declare @RequestMapping in the below format.

@RequestMapping(value = {"/aaa", "/bbb"}, method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
Pradeep K M
  • 1,461
  • 2
  • 10
  • 17