【发布时间】:2020-03-31 09:07:49
【问题描述】:
我尝试像这样POST:curl -X POST -H "Content-Type: application/json" -d "{ \"fisrtName\":\"sunny\" , \"lastName\":\"leone\" }" http://localhost:8080/user
但我收到此错误:{"timestamp":"2019-12-06T11:07:17.196+0000","status":405,"error":"Method Not Allowed","message" :"不支持请求方法'POST'","path":"/user"}
但是,我有这样的休息控制器:
@PostMapping("/user/{firstName}/{lastName}")
User addUser(@PathVariable String firstName,@PathVariable String lastName)
{
return userRepository.save(new User(firstName,lastName));
}
如何解决?
【问题讨论】:
-
好吧,看看你的 PostMapping。路径是什么?是您在 curl 请求中使用的
/user吗?当您在 curl 请求中发送时,该方法是否需要 JSON 请求正文? -
我建议使用 Postman 之类的工具来使用 REST url,如果您刚开始,它会更容易理解。
标签: spring spring-boot