[英]need explanation about rest PUT method
here is some controller: 这是一些控制器:
@RequestMapping(value="/rest/put/{login}", method = RequestMethod.PUT)
public @ResponseBody User putUser(@RequestBody User user, @PathVariable String login){
return user;
}
when i send this request 当我发送此请求时
{"login":"ale"}
to this URL 到这个URL
http://localhost:8080/Application/rest/put/termination
i receive normal response like this: 我收到这样的正常响应:
{"login":"ale","password":"password","email":"email"}
My question: why in controller must be @PathVariable login(at least it is in all tutorials) instead just static URL, and what the reason be here it? 我的问题:为什么在控制器中必须使用@PathVariable登录名(至少在所有教程中都是这样)而不是静态URL,请问这是什么原因?
Firstly, your question is not quite accurate. 首先,您的问题不太准确。 It is not a must to include
@PathVariable
inside the controller method. 这不是必须的 ,以包括
@PathVariable
控制器方法内。 You can use static URL whenever you like. 您可以随时使用静态URL。
Secondly, it is not a must to put @PathVariable
for REST, but a standard . 其次,不要把一个必须
@PathVariable
的休息,但一个标准 。 The standard is trying to map common CRUD operations with common HTTP verbs ( POST, GET, PUT, DELETE ), and the URL would typically include the resource name and resource ID. 该标准正在尝试使用常见的HTTP动词( POST,GET,PUT,DELETE )映射常见的CRUD操作,并且URL通常包含资源名称和资源ID。 Usually,
@PathVariable
represents resource ID in REST-standard URL. 通常,
@PathVariable
代表REST标准URL中的资源ID。
An example of such URL would be /user/{user_id}
, where user
is the resource name and user_id
would be the resource id. 这样的URL的示例是
/user/{user_id}
,其中user
是资源名称,而user_id
是资源ID。
So, lastly, by looking at the code you posted. 因此,最后,通过查看您发布的代码。 That
@PathVariable String login
does not really align with the REST standard. @PathVariable String login
实际上与REST标准不一致。 As in your example URL, 就像您的示例网址一样,
http://localhost:8080/Application/rest/put/termination
That means login = termination
, which is obviously nothing related to REST. 这意味着
login = termination
,这显然与REST无关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.