如何避免Spring中@RequestMapping路径冲突?

3

我希望使用Spring定义REST API。 我有以下两个@Controller方法:

@RequestMapping(value = "/{machineId}/workspaces/{workspaceId}", method = RequestMethod.DELETE)
public void removeWorkspace(@PathVariable String machineId, @PathVariable String workspaceId {...}

@RequestMapping(value = "/{machineId}", method = RequestMethod.DELETE)
public void removeVM(@PathVariable String machineId) {...}

问题在于,这个定义会导致路径冲突:如果我尝试调用
curl -X DELETE http://localhost/machine-1/workspaces/workspace-1

我认为应该调用"removeWorkspace"方法,但问题是却调用了"removeVM"方法。有没有人有什么好的想法来避免这种冲突? "{machineId}"前缀可能是导致冲突的原因。当然我希望保持这些路径不变。
谢谢。

哪个异常?没有异常,只是Spring调用了错误的方法。 - Jan
最佳匹配。正在调查中... - Artem Bilan
感谢测试。我正在使用Spring 3.1.1。 - Jan
似乎有一个问题已经被解决了,但很抱歉,我找不到它。 - Artem Bilan
尝试将第二个@RequestMapping(value = "/{machineId}"更新为@RequestMapping(value = "/{machineId}*",然后告诉我结果 :) - andy
显示剩余4条评论
2个回答

1

这是一个错误,在4.1-RC2版本中已经修复,更多详情请参见JIRA SPR-10576

因此,似乎您当前版本唯一解决问题的方法是更改URL路径或手动应用补丁。


0
你可以尝试使用正则表达式来告诉Spring,@RequestMapping(value = "/{machineId}", method = RequestMethod.DELETE)不应该匹配http://localhost/machine-1/workspaces/workspace-1 我以前没有尝试过这个方法,但也许对你有用:
  @RequestMapping(value = "/{machineId:[^/]*}", method = RequestMethod.DELETE)
  public void removeVM(@PathVariable String machineId) {...}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接