是否可以在一个方法上使用多个@RequestMapping注释?
如:
@RequestMapping("/")
@RequestMapping("")
@RequestMapping("/welcome")
public String welcomeHandler(){
return "welcome";
}
是否可以在一个方法上使用多个@RequestMapping注释?
如:
@RequestMapping("/")
@RequestMapping("")
@RequestMapping("/welcome")
public String welcomeHandler(){
return "welcome";
}
当前回答
最短的方法是:@RequestMapping({"", "/", "welcome"})
虽然你也可以这样做:
@ requestmaing(价值=“欢迎”) @ requestmaing(路径= "
其他回答
不需要。RequestMapping注释支持通配符和蚁式路径。看起来你只是想要一个默认视图,所以你可以
<mvc:view-controller path="/" view-name="welcome"/>
在配置文件中。这将把对根目录的所有请求转发到欢迎视图。
@RequestMapping有一个String[]值参数,所以你应该能够像这样指定多个值:
@RequestMapping(value={"", "/", "welcome"})
如果你仍然想要得到被调用的uri,最好使用PathVariable注释。
@PostMapping("/pub/{action:a|b|c}")
public JSONObject handlexxx(@PathVariable String action, @RequestBody String reqStr){
...
}
或者从请求对象解析它。
最短的方法是:@RequestMapping({"", "/", "welcome"})
虽然你也可以这样做:
@ requestmaing(价值=“欢迎”) @ requestmaing(路径= "
现在使用Spring-Boot 2.0.4 -{}不能工作。
@RequestMapping
仍然使用String[]作为值参数,因此声明如下所示:
@RequestMapping(value=["/","/index","/login","/home"], method = RequestMethod.GET)
**更新- Spring-Boot 2.2**
@RequestMapping(value={"/","/index","/login","/home"}, method = RequestMethod.GET)