Why is DELETE not supported on to-many-association resources in Spring Data REST?

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-10 20:15:06

问题


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.

To do this, I am using the DELETE method:

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

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.

My entity looks like this:

@Document
public class Category { 

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

回答1:


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

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.

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.

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.



来源:https://stackoverflow.com/questions/28679934/why-is-delete-not-supported-on-to-many-association-resources-in-spring-data-rest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!