需要解释休息PUT方法

时间:2015-01-16 08:58:49

标签: java spring rest put

这里有一些控制器:

@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"}

我的问题:为什么在控制器中必须是@PathVariable登录(至少在所有教程中)而不仅仅是静态URL,原因是什么呢?

1 个答案:

答案 0 :(得分:1)

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

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

此类网址的示例为/user/{user_id},其中user为资源名称,user_id为资源ID。

所以,最后,通过查看您发布的代码。 @PathVariable String login与REST标准并不完全一致。与示例网址中一样,

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

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

相关问题