【问题标题】:Can't perform PUT command in PostMan无法在 PostMan 中执行 PUT 命令
【发布时间】: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

【问题讨论】:

标签: java spring mongodb postman


【解决方案1】:

您可以通过在 application.properties 文件中添加以下配置来检查日志中映射的 api:

logging.level.org.springframework.web.servlet.mvc.method.annotation=TRACE

例如: 我有一个控制器:

@RestController
@RequestMapping("/client")
public class HomeRestController {

    @PutMapping("/{id}")
    public void put(@PathVariable(value = "id") String id, @RequestBody TestingModel model) {
        System.out.println(id);
        System.out.println(model.getName());
    }
}

启动应用程序时,您可以在控制台日志中看到映射的 API,如下所示:

    2020-07-14 09:36:49.287 TRACE 13224 --- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : 
    c.e.e.c.HomeController:
    { /index}: home()
2020-07-14 09:36:49.288 TRACE 13224 --- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : 
    c.e.e.c.HomeRestController:
    {PUT /client/{id}}: put(String,TestingModel)
2020-07-14 09:36:49.293 TRACE 13224 --- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : 
    o.s.b.a.w.s.e.BasicErrorController:
    { /error}: error(HttpServletRequest)
    { /error, produces [text/html]}: errorHtml(HttpServletRequest,HttpServletResponse)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-22
    • 1970-01-01
    • 2016-06-22
    • 2014-01-14
    • 2014-11-20
    • 2013-11-08
    • 1970-01-01
    相关资源
    最近更新 更多