gpt4 book ai didi

java - Spring Data Rest - PUT 存储库在子引用上静默失败

转载 作者:行者123 更新时间:2023-11-30 07:42:35 25 4
gpt4 key购买 nike

我将 Spring Data RestSpring Boot 2.1.1.RELEASE 一起使用。

我有一个类 User 与一个类 Skill 有一个 @ManyToMany 关系。

  • 当我使用他的技能创建一个 POST 用户时,一切正常。
  • 当我执行 PUT 更新用户时,技能不会更新,不会产生错误。
  • 但是当我创建一个 PATCH 而不是 PUT 时,技能会正确更新。

有没有人遇到过类似的问题?我发现了另一个(旧的)问题,但没有解决方案(Spring Data Rest - PUT is not working for associated reference types?)

我可能在某处遗漏了一些东西......

(使用 Lombok 的代码)

@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class User {

@Id
@GeneratedValue
private Long id;

private String firstName;
private String lastName;

@ManyToMany
@JoinTable(name="user_skills")
private List<Skill> skills = new ArrayList<>();

}

@Entity
@Getter
@Setter
@NoArgsConstructor
@ToString
public class Skill {

@Id
@GeneratedValue
private Long id;

private String name;
}

我用以下 JSON 内容制作了一个 PUT:

{
"id": 7,
"firstName": "John",
"lastName": "Doe",
"skills": ["http://localhost:9001/skills/1", "http://localhost:9001/skills/2", "http://localhost:9001/skills/3"]
}

可以修改名字或姓氏,但技能保持不变。

如果我使用相同的有效负载执行 PATCH,技能将被正确修改。

它应该与 PUT 一起工作,不是吗?

最佳答案

经过更多调查,这种行为似乎是故意的:PUT 不更新资源链接,只更新主要属性。

Oliver Gierke 的答案在这里: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.

其他帖子和链接:

关于java - Spring Data Rest - PUT 存储库在子引用上静默失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54445706/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com