Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
213 views
in Technique[技术] by (71.8m points)

java - Spring Data REST - PUT request does not work properly since v.2.5.7

Since version 2.5.7 Spring Data REST does not properly perform a PUT request to update resource which has associated resources. Unlike PATCH request that works as expected!

For example, Person has a many-to-one association with Addres. If we perform a PUT request with SDR v.2.5.6 (Spring Boot v.1.4.3) then all works OK. But if we switch to version 2.5.7 (i.e. to Spring Boot v.1.4.4) then we get an error:

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

The same happens with other types of associations, for example with one-to-many (uni- and bidirectional) - see my example application code and tests.

This problem is present in all versions of the Spring Boot since 1.4.4 including the latest stable 1.5.6 version, as well as the newest 2.0.0-SNAPSHOT version!

To work around this situation we can just switch to SDR v.2.5.6 (Spring Boot v.1.4.3).

I've prepared a Postman collection of requests to help you play with the issue: SDR PUT Issue

UPDATE 2017-08-14

I found how to avoid the error Can not construct instance of Address: no String-argument constructor/factory method to deserialize from String value.

Since I'm using Lombok in this project, it is necessary just to tell Lombok to suppress using the @ConstructorProperties annotation in generated constructors. So I set lombok.anyConstructor.suppressConstructorProperties=true in the 'lombok.config' file and the error was gone.

Unfortunately a new problem was found - PUT request does not update associated objects at all!

The example below is demonstrating this. When we are trying to update Person by changing his Address from addresses/1 (initial value) to addresses/2 - then it remains the same: addresses/1! As well as the previous problem this one is present in all versions of the Spring Boot since 1.4.4 (SDR - from v.2.5.7).

I debugged my project and found out that the reason of the issue is hidden in the method DomainObjectReader#mergeForPut (see its source) - it never replaces associated resources with new ones.

Before I post this issue on Spring JIRA, please report here if you have this issue in your projects and what do you think about it.

You can get my test here and check it in your projects - the test is 'standalone' and doesn't depend from other classes/modules (exclude only H2, I hope).

@Entity
public class Person {

    private String name;

    @ManyToOne
    private Address address;

    // other stuff
}

@Entity    
public class Address {

    private String street;

    // other stuff
}

Trying to update Person:

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

Getting the correct response:

{
    "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"
        }
    }
}

Then checking for a 'new' Address of Person - address was not updated:

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"
        }
    }
}

UPDATE 2017-08-24

Thanks to Scott C. answer, it turned out that SDR has a bug, which is described in two tickets: DATAREST-1001 and DATAREST-1012.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Looks like the issue has already been reported as a bug: - please verify. As best as I can tell, this is the issue you are reporting above.

Note, I am revising my previous answer to be this bug report.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...