这是一些 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/