Java spring中如何使用多个@RequestMapping注解?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2513031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:40:47  来源:igfitidea点击:

How to use multiple @RequestMapping annotations in spring?

javaspringspring-mvc

提问by wuntee

Is it possible to use multiple @RequestMappingannotations over a method?

是否可以@RequestMapping在一个方法上使用多个注释?

Like :

喜欢 :

@RequestMapping("/")
@RequestMapping("")
@RequestMapping("/welcome")
public String welcomeHandler(){
  return "welcome";
}

采纳答案by Ed Brannin

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

@RequestMapping有一个String[]value 参数,因此您应该能够指定多个值,如下所示:

@RequestMapping(value={"", "/", "welcome"})

@RequestMapping(value={"", "/", "welcome"})

回答by Robby Pond

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

没必要。RequestMapping 注解支持通配符和蚂蚁风格的路径。看起来你只想要一个默认视图,所以你可以把

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

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

在您的配置文件中。这会将所有请求转发到 Root 到欢迎视图。

回答by Alan Zhong

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.

从我的测试(spring 3.0.5)来看,@RequestMapping(value={"", "/"})- 仅"/"有效,""无效。但是我发现这有效:@RequestMapping(value={"/", " * "})" * "匹配任何内容,因此如果没有其他处理程序,它将成为默认处理程序。

回答by Falcon

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

现在使用 Spring-Boot 2.0.4 - { } 将不起作用。

@RequestMapping

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

仍然有 String[] 作为值参数,所以声明看起来像这样:

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

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

** 更新 - 适用于 Spring-Boot 2.2**

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

回答by CQLI

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

如果您仍然想获取被调用的 uri,最好使用 PathVariable 注释。

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

or parse it from request object.

或从请求对象解析它。

回答by Marco

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

最短的方法是: @RequestMapping({"", "/", "welcome"})

Although you can also do:

虽然你也可以这样做:

  • @RequestMapping(value={"", "/", "welcome"})
  • @RequestMapping(path={"", "/", "welcome"})
  • @RequestMapping(value={"", "/", "welcome"})
  • @RequestMapping(path={"", "/", "welcome"})

回答by Pritam Banerjee

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 @RequestMappingas well

同样可以应用于@RequestMapping以及