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.

3
  • Under what condition will you need this? Commented Dec 14, 2013 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 Commented Sep 2, 2014 at 9:25
  • Duplicate to: Multiple Spring @RequestMapping annotations Commented Jul 30, 2015 at 6:32

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This is exactly what I wanted. Thanks a lot. One more request. How can I add one more mapping in below method<br> @RequestMapping(value = "/add", method = RequestMethod.POST)
got it...no need to reply
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")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.