gpt4 book ai didi

java - Spring Data REST - PUT 请求自 v.2.5.7 起无法正常工作

转载 作者:搜寻专家 更新时间:2023-10-30 21:10:30 26 4
gpt4 key购买 nike

2.5.7 版本以来,Spring Data REST 无法正确执行 PUT 请求来更新具有关联资源 的资源。与按预期工作的 PATCH 请求不同!

例如,PersonAddres 具有多对一关联。如果我们使用 SDR v.2.5.6(Spring Boot v.1.4.3)执行 PUT 请求,那么一切正常。但是,如果我们切换到版本 2.5.7(即 Spring Boot v.1.4.4),则会出现错误:

Can not construct instance of Address: no String-argument constructor/factory method to deserialize from String value

其他类型的关联也会发生同样的情况,例如一对多(单向和双向)——请参阅我的 example application 代码和测试。

自 1.4.4 以来的 所有 Spring Boot 版本都存在此问题,包括最新的稳定版 1.5.6 以及最新的 2.0.0-SNAPSHOT 版本!

要解决这种情况,我们可以切换到 SDR v.2.5.6(Spring Boot v.1.4.3)。

我准备了一个 Postman 请求集合 来帮助您解决这个问题:SDR PUT Issue

2017-08-14 更新

我找到了如何避免错误 Can not construct instance of Address: no String-argument constructor/factory method to deserialize from String value

因为我正在使用 Lombok在这个项目中,只需告诉 Lombok 禁止使用 @ConstructorProperties 注释 generated constructors .所以我在“lombok.config”文件中设置了 lombok.anyConstructor.suppressConstructorProperties=true,错误消失了。

很遗憾,发现了一个新问题 - PUT 请求根本不更新关联对象!

下面的例子证明了这一点。当我们尝试通过将他的地址从 addresses/1 (初始值)更改为 addresses/2 来更新 Person - 然后它保持不变:addresses/1 !除了之前的问题,自 1.4.4(SDR - 从 v.2.5.7 起)以来,所有 Spring Boot 版本都存在这个问题。

我调试了我的项目,发现问题的原因隐藏在方法 DomainObjectReader#mergeForPut 中(参见 its source )——它从不替换相关资源与新的。

在我将此问题发布到 Spring JIRA 之前,请在您的项目中遇到此问题并在此处报告您的想法

你可以得到我的测试here并在您的项目中检查它 - 测试是“独立的”并且不依赖于其他类/模块(我希望仅排除 H2)。

@Entity
public class Person {

private String name;

@ManyToOne
private Address address;

// other stuff
}

@Entity
public class Address {

private String street;

// other stuff
}

尝试更新人物:

PUT http://localhost:8080/api/persons/1
{
"name": "person1u",
"address": "http://localhost:8080/api/addresses/2"
}

得到正确的响应:

{
"name": "person1u",
"_links": {
"self": {
"href": "http://localhost:8080/api/persons/1"
},
"person": {
"href": "http://localhost:8080/api/persons/1"
},
"address": {
"href": "http://localhost:8080/api/persons/1/address"
}
}
}

然后检查人员的"new"地址 - 地址未更新:

GET http://localhost:8080/api/persons/1/address
{
"street": "address1",
"_links": {
"self": {
"href": "http://localhost:8080/api/addresses/1"
},
"address": {
"href": "http://localhost:8080/api/addresses/1"
}
}
}

2017-08-24 更新

感谢 Scott C. answer ,结果发现 SDR 有一个bug,在两张票中都有描述:DATAREST-1001DATAREST-1012 .

最佳答案

看起来问题有 already been reported as a bug : - 请验证。据我所知,这就是您在上面报告的问题。

请注意,我正在将我之前的答案修改为此错误报告。

关于java - Spring Data REST - PUT 请求自 v.2.5.7 起无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45620195/

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