300

Is it possible to use multiple @RequestMapping annotations over a method?

Like :

@RequestMapping("/")
@RequestMapping("")
@RequestMapping("/welcome")
public String welcomeHandler(){
  return "welcome";
}
Anish B.
  • 9,111
  • 3
  • 21
  • 41
wuntee
  • 12,170
  • 26
  • 77
  • 106

7 Answers7

524

@RequestMapping has a String[] value parameter, so you should be able to specify multiple values like this:

@RequestMapping(value={"", "/", "welcome"})
2240
  • 1,547
  • 2
  • 12
  • 30
Ed Brannin
  • 7,691
  • 2
  • 28
  • 32
  • 2
    That said, I'm having trouble getting the "" or "/" values to actually work in my application. Do they work for you? – Ed Brannin May 12 '10 at 19:15
  • Is there a way to associate different success views and form views with each request URL using multiple annotations? – k-den Mar 16 '16 at 20:25
  • @EdBrannin I need many to use, custom, header, consumes, params, etc – deFreitas May 09 '16 at 19:01
  • Also I would like to know, how do I know which requestmapping has been called. is it / or welcome ? – Siddharth Aug 30 '17 at 10:53
  • 1
    @Siddharth 1. You might be able to add & inspect a parameter of type HttpRequest. 2. If you really care which mapping was called, maybe don't use this technique. ;) – Ed Brannin Aug 30 '17 at 11:57
  • For all trying to reproduce this in kotlin, you need to use arrayOf or [] instead of the {} syntax. – Franz Aug 08 '22 at 10:17
27

From my test (spring 3.0.5), @RequestMapping(value={"", "/"}) - only "/" works, "" does not. However I found out this works: @RequestMapping(value={"/", " * "}), the " * " matches anything, so it will be the default handler in case no others.

Benjamin
  • 11,560
  • 13
  • 70
  • 119
Alan Zhong
  • 387
  • 4
  • 7
14

Doesn't need to. RequestMapping annotation supports wildcards and ant-style paths. Also looks like you just want a default view, so you can put

<mvc:view-controller path="/" view-name="welcome"/>

in your config file. That will forward all requests to the Root to the welcome view.

skaffman
  • 398,947
  • 96
  • 818
  • 769
Robby Pond
  • 73,164
  • 16
  • 126
  • 119
  • Is there supposed to be something between those two lines? I am using the FreeMarkerViewResolver - so I would have to go this way... Well, I guess I could just create multiple ViewResolver. – wuntee Mar 25 '10 at 14:28
  • 2
    It doesn't provide the flexibility that multiple RequestMapping annotations would provide. For example, if I want to have one method support either value "/a" with POST or value "/b" with GET. Of course the workaround is fairly easy (refactoring the functionality in a third method), but just saying that it would be useful. – simon Sep 25 '14 at 14:08
8

The shortest way is: @RequestMapping({"", "/", "welcome"})

Although you can also do:

  • @RequestMapping(value={"", "/", "welcome"})
  • @RequestMapping(path={"", "/", "welcome"})
Marco
  • 1,377
  • 15
  • 18
7

The following is acceptable as well:

@GetMapping(path = { "/{pathVariable1}/{pathVariable1}/somePath", 
                     "/fixedPath/{some-name}/{some-id}/fixed" }, 
            produces = "application/json")

Same can be applied to @RequestMapping as well

Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
4

It's better to use PathVariable annotation if you still want to get the uri which was called.

@PostMapping("/pub/{action:a|b|c}")
public JSONObject handlexxx(@PathVariable String action, @RequestBody String reqStr){
...
}

or parse it from request object.

CQLI
  • 413
  • 4
  • 11
2

Right now with using Spring-Boot 2.0.4 - { } won't work.

@RequestMapping

still has String[] as a value parameter, so declaration looks like this:

 @RequestMapping(value=["/","/index","/login","/home"], method = RequestMethod.GET)

** Update - Works With Spring-Boot 2.2**

 @RequestMapping(value={"/","/index","/login","/home"}, method = RequestMethod.GET)
Falcon
  • 338
  • 3
  • 9