【问题标题】:How to use Spring method DELETE如何使用 Spring 方法 DELETE
【发布时间】:2022-07-10 23:09:16
【问题描述】:

我在 ArrayList 中有一些客户,当我想通过 Postman 删除(带 id)时它不起作用:500 - 内部错误。请问有人可以帮我吗?

删除客户
删除 http://localhost:8080/api/customers/{customerId}

@DeleteMapping("/api/customers{customerId}")
public void deleteCustomer(@PathVariable  Long customerId) {
    customers.remove(this.customers.get(Math.toIntExact(customerId)));

}

【问题讨论】:

  • 到目前为止的答案表明罪魁祸首是您的路径(在客户之后缺少斜线)。这绝对是一个问题,但这不会导致 500 内部服务器错误。分享错误的内容,只有这样才能回答这个问题......是的,你也必须修复那个路径。

标签: spring rest http-delete


【解决方案1】:

这样试试你没有在删除映射中添加"/"

@DeleteMapping("/api/customers/{customerId}")
public void deleteCustomer(@PathVariable  Long customerId) {
    customers.remove(this.customers.get(Math.toIntExact(customerId)));

}

【讨论】:

    【解决方案2】:

    这是您发送请求的方式吗?

    http://localhost:8080/api/customers/{customerId}
    

    如果你这样发送,它不会起作用,因为你给了这样的路径:

    /api/customers{customerId}
    

    在我看来,要么像这样改变路径:

    /api/customers/{customerId} 
    

    或者像这样发送请求:

    http://localhost:8080/api/customers{customerId}
    

    【讨论】:

      猜你喜欢
      • 2015-05-08
      • 2020-01-20
      • 2017-10-19
      • 2016-11-01
      • 2011-05-29
      • 2014-04-25
      • 1970-01-01
      • 1970-01-01
      • 2018-04-23
      相关资源
      最近更新 更多