首页 > 解决方案 > 如何在不知道 json 中的 id 的情况下发送删除请求?

问题描述

例如,如果我使用了 findByEmail,我的 url 将类似于 http://localhost:8080/api/carts/search/findByEmail?email=helloworld@gmail.com

如果我在 JPA 存储库中有 deleteByEmail,删除请求的 URL 将如何显示

这就是我的 JPA 的样子

public interface CartRepository extends JpaRepository<Cart, Integer> {

    @Transactional
    @RestResource(exported = true)
    List<Cart> deleteByEmail(String email);
}

标签: jsonapispring-bootspring-data-jpa

解决方案


您可以拥有一个作为http://localhost:8080/api/carts/email/ {emailId} 的 REST API,并将 PathVariable HttpMethod 中的 emailId 作为 DELETE。在服务方法中,您通过 emailId 搜索,然后使用 ID 删除它。


推荐阅读