Spring Data Rest - PUT on repository silently fails on child references

后端 未结 1 1519
遇见更好的自我
遇见更好的自我 2021-01-21 13:45

I use Spring Data Rest with Spring Boot 2.1.1.RELEASE.

I have a class User with a @ManyToMany relationship to a class

相关标签:
1条回答
  • 2021-01-21 14:47

    After more investigations, it seems that this behaviour is by purpose: PUT does not update the resource links, only the main attributes.

    The answer from Oliver Gierke is here: https://jira.spring.io/browse/DATAREST-1001?focusedCommentId=135791&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-135791:

    I looked into this and I'd argue you're expecting things to work in a way they don't work. PUT requests don't consider associations to linkable resources, i.e. related resources that are pointed to by links. The reason for that is two-fold:

    1. If we consider URIs for association fields in the payload to update those associations, the question comes up about what's supposed to happen if no URI is specified. With the current behavior, linked associations are simply not a part of the payload as they only reside in the _links block. We have two options in this scenario: wiping the associations that are not handed, which breaks the "PUT what you GET" approach. Only wiping the ones that are supplied using null would sort of blur the "you PUT the entire state of the resource".
    2. For all the reasons mentioned in 1. there are dedicated assoctiation resources exposed that can be manipulated directly.

    So it looks like that if you want to change both state of the resource plus associations at the same time, I guess exposing a dedicated resource to do that is the way to go.

    Other posts and links:

    • "Unable to update associated resource using PUT request on the item resource": https://jira.spring.io/browse/DATAREST-1001
    • "Spring Data Rest PUT v.s PATCH LinkableResources" : Spring Data Rest PUT v.s PATCH LinkableResources
    • "PUT behaving like PATCH for nested collections": https://jira.spring.io/browse/DATAREST-1012
    0 讨论(0)
提交回复
热议问题