java - 资源名称中的 spring-way 句柄斜线

标签 java spring rest url

我在 Controller 中有以下方法:

 @RequestMapping(value = "/items/{identifier}", method = RequestMethod.DELETE)
    public void delete(@PathVariable String identifier) {
    ... 
    // find item in database by identifier
}

问题是标识符是一个可以包含斜线 ('/')String。 IE。可能发生的情况:

http://myhost/items/file:/123

对于给定的 URL,“标识符”必须是“file:/123”。

如果有 '/',则 405(不允许使用该方法)会因 URL 不正确而抛出。

如何告诉 Spring 它应该将 '/items/' 之后的所有内容作为“标识符”(REST 术语中的资源名称)?

编辑: 有人告诉我管理斜杠和编码是客户的责任,所以我保留原样。

但对于信息,基于 ankur-singhal在评论中回答:

我想要的支持已在 Spring 4.1.0 中添加,请参阅 this link还有这个commit . 所以就我而言,它将是:

 @RequestMapping(value = "/items/{/identifier}", method = RequestMethod.DELETE)

'identifier' 之前的'/' 做了个把戏。

最佳答案

尝试将代码更改为下面的代码,看看是否有效

它将接受identifier 的任何值。

 @RequestMapping(value = "/items/{identifier:.*}", method = RequestMethod.DELETE)
    public void delete(@PathVariable String identifier) {
    ... 
    // find item in database by identifier
}

如果使用identifier作为request parameter那么可以实现如下:

 @RequestMapping(value = "/items", method = RequestMethod.DELETE)
    public void delete(@RequestParam("identifier") String identifier) {
    ... 
    // find item in database by identifier
}

请引用here了解更多信息。

关于java - 资源名称中的 spring-way 句柄斜线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25782059/

相关文章:

java - 用于选择特定列的 Spring Data JPA 规范

python - djangorest框架授权返回错误的状态代码

javascript - Angular,HTTP Get 有效,Post 收到 CORS 错误

java - 将列表的第一个元素输入数组的最快方法

c# - 访问实例变量时使用关键字this是不是效率更高?

java - 将 java 类路径设置为包含 jar 文件并从命令行运行的多个文件夹

java - 我不知道这个线程有什么问题,我想了解多线程

java - 使用 Spring 时如何抑制 checkstyle 消息 "Utility classes should not have a public of default constructor"

javascript - 后台有 mysql 数据库的 Spring 应用程序的 ICD 10 下拉列表

rest - 如何使用不可变资源处理 PUT 端点