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

Cep*_*pr0 13 java rest put spring-data-rest spring-boot

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

例如,Person与多对一关联Addres.如果我们使用SDR v.2.5.6(Spring Boot v.1.4.3)执行PUT请求,那么一切正常.但是,如果我们切换到2.5.7版(即Spring Boot v.1.4.4),那么我们会收到一个错误:

无法构造Address的实例:没有String-argument构造函数/工厂方法从String值反序列化

其他类型的关联也会发生同样的情况,例如一对多(单向和双向) - 请参阅我的示例应用程序代码和测试.

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

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

我准备了一个邮递员的请求集合,以帮助您解决问题:SDR PUT问题

更新2017-08-14

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

由于我在这个项目中使用Lombok,因此有必要告诉Lombok禁止@ConstructorProperties生成的构造函数中使用注释 .所以我lombok.anyConstructor.suppressConstructorProperties=true在'lombok.config'文件中设置,错误消失了.

不幸的是,发现了一个新问题 - PUT请求根本不更新关联的对象!

以下示例说明了这一点.当我们尝试通过将他的地址从addresses/1(初始值)更改为addresses/2- 来更新Person时,它仍然是相同的:addresses/1!与之前的问题一样,自1.4.4以来,这一问题出现在Spring Boot的所有版本中(SDR - 来自v.2.5.7).

我调试了我的项目,发现问题的原因隐藏在方法中DomainObjectReader#mergeForPut(参见其来源) - 它永远不会用新的资源替换相关资源.

在我在Spring JIRA上发布此问题之前,如果您的项目中存在此问题,请在此处报告,您对此有何看法.

你可以在这里得到我的测试并在你的项目中检查它 - 测试是'独立的'并且不依赖于其他类/模块(我希望只排除H2).

@Entity
public class Person {

    private String name;

    @ManyToOne
    private Address address;

    // other stuff
}

@Entity    
public class Address {

    private String street;

    // other stuff
}
Run Code Online (Sandbox Code Playgroud)

试图更新人:

PUT http://localhost:8080/api/persons/1
Run Code Online (Sandbox Code Playgroud)
{
    "name": "person1u",
    "address": "http://localhost:8080/api/addresses/2"
}
Run Code Online (Sandbox Code Playgroud)

得到正确答案:

{
    "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"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

GET http://localhost:8080/api/persons/1/address
Run Code Online (Sandbox Code Playgroud)
{
    "street": "address1",
    "_links": {
        "self": {
            "href": "http://localhost:8080/api/addresses/1"
        },
        "address": {
            "href": "http://localhost:8080/api/addresses/1"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

更新2017-08-24

感谢Scott C. 回答,结果发现SDR有一个错误,在两张票中有描述:DATAREST-1001DATAREST-1012.

Sco*_* C. 7

看起来这个问题已经被报告为一个错误: - 请验证。据我所知,这就是您在上面报告的问题。

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