java - 需要有关剩余 PUT 方法的解释

标签 java spring rest put

这是一些 Controller :

@RequestMapping(value="/rest/put/{login}", method = RequestMethod.PUT)
    public @ResponseBody User putUser(@RequestBody User user, @PathVariable String login){
        return user;
    }

当我发送此请求时

{"login":"ale"}

到此网址

http://localhost:8080/Application/rest/put/termination

我收到这样的正常回复:

{"login":"ale","password":"password","email":"email"}

我的问题:为什么在 Controller 中必须是 @PathVariable 登录(至少在所有教程中都是如此)而不是静态 URL,这是什么原因?

最佳答案

首先,你的问题不太准确。在 Controller 方法内包含 @PathVariable 并不是必须。您可以随时使用静态 URL。

其次,对于 REST,放置 @PathVariable 并不是必须,而是标准。该标准试图将常见的 CRUD 操作与常见的 HTTP 动词(POST、GET、PUT、DELETE)进行映射,并且 URL 通常包括资源名称和资源 ID。通常,@PathVariable 表示 REST 标准 URL 中的资源 ID。

此类 URL 的示例为 /user/{user_id},其中 user 是资源名称,user_id 是资源id。

最后,通过查看您发布的代码。 @PathVariable String login 并不真正符合 REST 标准。如您的示例网址所示,

http://localhost:8080/Application/rest/put/termination

这意味着login=termination,这显然与REST无关。

关于java - 需要有关剩余 PUT 方法的解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27980289/

相关文章:

java - 在 Netbeans Java 应用程序中,如何通过单击按钮打开新页面(不是弹出窗口)?

Spring 中启用 Apache Shiro 注释时未找到 Spring 数据 JPA 存储库

java - 基准测量方法拦截框架

rest - Golang 服务响应文件

java - 请求参数的存在应评估为真

java - HashMap - 获取 "Can only iterate over an array or an instance of java.lang.Iterable"

Java FX 潜水整数属性

java - Spring RestTemplate 接收 sun.security.validator.ValidatorException : PKIX path building failed:

java - TALEND:如何存储 tRest 组件中所需的刷新 token ?

php - 可以一起使用 Zend_Http_Client() 和 Zend_Rest_Client()