Java Spring Web MVC:对请求参数和路径变量使用相同的请求映射
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2745471/
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
Spring Web MVC: Use same request mapping for request parameter and path variable
提问by ngeek
Is there a way to express that my Spring Web MVC controller method should be matched either by a request handing in a ID as part of the URI path ...
有没有办法表达我的 Spring Web MVC 控制器方法应该通过请求将 ID 作为 URI 路径的一部分进行匹配...
@RequestMapping(method=RequestMethod.GET, value="campaigns/{id}")
public String getCampaignDetails(Model model, @PathVariable("id") Long id) {
... or if the client sends in the ID as a HTTP request parameter in the style ...
... 或者如果客户端将 ID 作为 HTTP 请求参数以样式发送 ...
@RequestMapping(method=RequestMethod.GET, value="campaigns")
public String getCampaignDetails(Model model, @RequestParam("id") Long id) {
This seems to me a quite common real-world URL scheme where I don't want to add duplicate code, but I wasn't able to find an answer yet. Any advice highly welcome.
在我看来,这是一个非常常见的现实世界 URL 方案,我不想添加重复的代码,但我还没有找到答案。非常欢迎任何建议。
EDIT:It turns out that there seems currently (with Spring MVC <= 3.0)no way to achieve this, see discussion inside Javi's answer.
编辑:事实证明,目前(使用 Spring MVC <= 3.0)似乎无法实现这一点,请参阅 Javi 答案中的讨论。
采纳答案by Javi
You can set both mapping url for the same function and setting id as optional.
您可以为同一功能设置映射 url,也可以将 id 设置为可选。
@RequestMapping(method=RequestMethod.GET, value={"/campaigns","/campaigns/{id}"})
public String getCampaignDetails(Model model,
@RequestParam(value="id", required=false) Long id,
@PathVariable("id") Long id2)
{
}
though it would map as well when id is not sent, but you can control this inside the method.
虽然在不发送 id 时它也会映射,但您可以在方法中控制它。
EDIT: The previous solution doesn't work because @PathVariable
is not set to null
when there isn't {null}
and it cannot map the URL (thanks ngeek). I think then that the only possible solution is to create two methods each one mapped with its @MappingRequest
and inside one of them call the other function or redirect to the other URL (redirect: or forward: Spring prefixes). I know this solution is not what you're looking for but think it's the best you can do. Indeed you're not duplicating code but you're creating another function to handle another URL.
编辑:以前的解决方案不起作用,因为@PathVariable
在没有null
时未设置为,{null}
并且无法映射 URL(感谢 ngeek)。我认为唯一可能的解决方案是创建两个方法,每个方法与其映射,@MappingRequest
其中一个方法调用另一个函数或重定向到另一个 URL(重定向:或转发:Spring 前缀)。我知道这个解决方案不是你想要的,但认为它是你能做的最好的。实际上,您不是在复制代码,而是在创建另一个函数来处理另一个 URL。
回答by Vivex
If you still want to stick to PathVariable approach and if you are getting 400 syntactically incorrect error then follow this approach-
如果您仍想坚持使用 PathVariable 方法,并且遇到 400 句法错误,请遵循以下方法-
@RequestMapping(method=RequestMethod.GET, value={"campaigns/{id}","campaigns"})
public String getCampaignDetails(Model model,
@PathVariable Map<String, String> pathVariables)
{
System.out.println(pathVariables.get("id"));
}
回答by Jpnh
The @RequestMapping
annotation now supports setting the path
attribute instead of name
or value
. With path
, you can achieve the mapping desired by this question:
该@RequestMapping
注释现在支持设置path
属性,而不是name
或value
。使用path
,您可以实现此问题所需的映射:
@RequestMapping(method=RequestMethod.GET, path="campaigns/{id}")
public String getCampaignDetails(Model model, @PathVariable("id") Long id) {
@RequestMapping(method=RequestMethod.GET, value="campaigns")
public String getCampaignDetails(Model model, @RequestParam("id") Long id) {