【发布时间】:2020-11-02 11:58:19
【问题描述】:
我是 Spring Boot 和 MongoDB 领域的新手,所以这可能是一个愚蠢的问题。
我创建了一个链接到 MondoDB 数据库的 Spring Boot 项目。在控制器中,我定义了这些方法:get、getAll、add、update 和 delete。
当我在 PostMan 上测试我的应用程序时,一切正常,除了更新方法。事实上,在 PostMan 中,使用 PUT 命令,我得到了这个错误:
“状态”:405, “错误”:“方法不允许”
寻找解决方案,我在 PostMan 中找到了这些行: PUT non allowed
其中“Allow”的值只包含“GET, DELETE”而不包含PUT。
也许这个事实与我的错误有关?我该如何解决?
感谢您,对我的英语不好和对 SpringBoot 缺乏了解感到抱歉!
编辑 1:控制器代码:
@PutMapping("/{id}")
public ResponseEntity <Cliente> updateCliente(@PathVariable(value = "id") String id, @RequestBody Cliente cliente){
Optional<Cliente> c = clienteRepo.findById(id);
Cliente _c = new Cliente();
if(c.isPresent()) {
_c = c.get();
_c.setId(cliente.getId());
_c.setNome(cliente.getNome());
}
final Cliente updatedCliente = clienteRepo.save(_c);
return ResponseEntity.ok(updatedCliente);
}
编辑 2:邮递员请求: PostMan
【问题讨论】:
-
可以分享控制器代码吗?它可能缺少 PUT 方法!或者你可能从 Postman 错误地调用了错误的方法!
-
你能说明你是如何在 Postman 上发出 PUT 请求的吗?
-
请粘贴代码,而不是带有代码的图片链接。您可以使用 Markdown 正确格式化代码,例如:github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet#code
标签: java spring mongodb postman