[英]How to use multiple @RequestMapping annotations in spring?
Is it possible to use multiple @RequestMapping
annotations over a method?是否可以在一个方法上使用多个
@RequestMapping
注释?
Like :喜欢 :
@RequestMapping("/")
@RequestMapping("")
@RequestMapping("/welcome")
public String welcomeHandler(){
return "welcome";
}
@RequestMapping
有一个String[]
值参数,因此您应该能够像这样指定多个值:
@RequestMapping(value={"", "/", "welcome"})
From my test (spring 3.0.5), @RequestMapping(value={"", "/"})
- only "/"
works, ""
does not.从我的测试(spring 3.0.5)来看,@
@RequestMapping(value={"", "/"})
- 只有"/"
有效, ""
无效。 However I found out this works: @RequestMapping(value={"/", " * "})
, the " * "
matches anything, so it will be the default handler in case no others.但是我发现这有效:
@RequestMapping(value={"/", " * "})
, " * "
匹配任何内容,因此如果没有其他处理程序,它将成为默认处理程序。
Doesn't need to.没必要。 RequestMapping annotation supports wildcards and ant-style paths.
RequestMapping 注解支持通配符和蚂蚁风格的路径。 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.
这会将所有请求转发到 Root 到欢迎视图。
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)
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.或从请求对象解析它。
The shortest way is: @RequestMapping({"", "/", "welcome"})
最短的方法是:
@RequestMapping({"", "/", "welcome"})
Although you can also do:虽然你也可以这样做:
@RequestMapping(value={"", "/", "welcome"})
@RequestMapping(path={"", "/", "welcome"})
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同样可以应用于
@RequestMapping
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.