为什么 Spring Data REST 中的多关联资源不支持 DELETE? [英] Why is DELETE not supported on to-many-association resources in Spring Data REST?

查看:30
本文介绍了为什么 Spring Data REST 中的多关联资源不支持 DELETE?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Spring Data REST.我正在尝试从实体(项目)解除集合关联.即 - 项目的属性是 List 类型.我想从该 List 中删除所有项目.

I am using Spring Data REST. I am trying to unbind a collection association from an entity (item). i.e. - a property of the item is of List type. I want to remove all items from that List.

为此,我使用的是 DELETE 方法:

To do this, I am using the DELETE method:

curl -X DELETE …/categories/54ea0bcf27a2fb1b4641083a/fixedParentCategories

这给了我一个 405 Method not allowed 错误代码.但是,它适用于单值关联(当它不是 List 类型时).文档 清楚地将 DELETE 列为支持的关联方法.我想知道是否有办法解决这个问题.另外,我尝试将 PUT (Content-Type: text/uri-list) 与空正文一起使用,它给出了有关缺少请求正文的错误.此关联上的其他操作都运行良好 - 我可以将项目添加到此集合等.

This gives me a 405 Method not allowed error code. But, it works for a single valued association (when it is not of List type). The documentation clearly lists DELETE as a supported method for associations. I'd like to know if there is a way around this. Also, I tried using PUT (Content-Type: text/uri-list) with an empty body, and it gives an error about missing request body. Other operations on this association are all working fine - I am able to add items to this collection, etc.

我的实体如下所示:

@Document
public class Category { 

    @DBRef(lazy = true)
    private List<Category> fixedParentCategories;
    …
}

推荐答案

我刚刚检查了代码,你是对的,我们正在积极拒绝 DELETE 请求 Maps 和收藏.理由如下:

I just checked the code and you're right, we're actively rejecting DELETE requests for Maps and collections. The rationale is as follows:

Map 或集合的关联在域模型中绝不能为 null.将其转换为 HTTP 资源意味着该资源将始终可用,并且在最坏的情况下返回一个空表示(空 JSON 数组或空 JSON 对象).接受 DELETE 请求将逻辑上 null 域模型中的关系,从而导致与第一个假设相矛盾的状态.

An association that is of Map or collection must never be null in the domain model. Translating this into HTTP resources means that the resource will always be available and in the worst case return an empty representation (empty JSON array, or empty JSON object). Accepting a DELETE request would logically null the relationship in the domain model and thus lead to a state that contradicts the very first assumption.

我们通常建议将媒体类型为 text/uri-list 的空正文简单地放入关联资源以清空关联,因为这在语义上更正确.将其视为截断和删除数据库表之间的区别.

We generally recommend to simply PUT an empty body with media type text/uri-list to the association resource to empty out the associations as that's semantically more correct. Consider it like the difference between truncating and dropping a database table.

如果您认为这应该改变并且有充分的理由支持您的请求,请随时在我们的 JIRA.

If you think that should change and have good reasons that you can back your request with, feel free to open a ticket in our JIRA.

这篇关于为什么 Spring Data REST 中的多关联资源不支持 DELETE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆