Christian Bongiorno
Christian Bongiorno

Reputation: 5648

Adding a resource to a collection using spring-data-rest

My situation: I have an Organization that can have many workers. I am trying to add a new worker to an organization following this example by the major contributing author of the SDR project and I get various errors (including 204 but nothing happens).

Here are my entities and rest calls:

@Entity
public class Organization {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;


    @OneToMany
    @JoinTable(name = "OrganizationWorker", joinColumns = {@JoinColumn(name = "OrganizationID")},
            inverseJoinColumns = {@JoinColumn(name = "WorkerID")})
    private Set<Worker> workers = new LinkedHashSet<>();
}

public class Worker {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;

    @NotNull
    private String givenName;

    @NotNull
    private String familyName;

    @NotNull
    private LocalDate dob;

    @NotNull
    private String nationalId;

    private byte[] photo;
}

GET http://localhost:8080/hal

{
    "_links": {

        "workers": {
            "href": "http://localhost:8080/hal/workers{?page,size,sort}",
            "templated": true
        }
    }
}

POST http://localhost:8080/hal/workers

{
 "givenName": "James",
    "familyName": "Bond",
    "dob": "1970-01-01",
    "nationalId": "XXX-60-XXXX",
    "photo": null,
}

Response:

Location: http://localhost:8080/hal/workers/8
Date: Mon, 02 May 2016 16:53:02 GMT
Server: Apache-Coyote/1.1
Transfer-Encoding: chunked
X-Application-Context: application
Content-Type: application/hal+json;charset=UTF-8


{
    "givenName": "James",
    "familyName": "Bond",
    "dob": "1970-01-01",
    "nationalId": "XXX-60-XXXX",
    "photo": null,
    "_links": {
        "self": {
            "href": "http://localhost:8080/hal/workers/8"
        },
        "worker": {
            "href": "http://localhost:8080/hal/workers/8"
        }
    }
}

Final step, as per link in description:

curl -X PUT -H "ContentType: text/uri-list" http://localhost:8080/hal/organizations/2 -d 'http://localhost:8080/hal/workers/8'

{
    "cause": null,
    "message": "Target bean is not of type of the persistent entity!"
}

Doing some debug it's pretty obvious what the specific complaint is. The stack trace leads here:

@Override
public PersistentPropertyAccessor getPropertyAccessor(Object bean) {

    Assert.notNull(bean, "Target bean must not be null!");
    Assert.isTrue(getType().isInstance(bean), "Target bean is not of type of the persistent entity!");

    return new BeanWrapper<Object>(bean);
}

getType() -> Organization

isInstance(bean) -> bean instance of org.springframework.hateoas.Resource

Any input on this? I followed the instructions to the letter.

Upvotes: 4

Views: 1972

Answers (1)

Christian Bongiorno
Christian Bongiorno

Reputation: 5648

Here is the answer (it took going out for a walk to clear my head and hit this).

You have to post to the association resource

http://localhost:8080/hal/organizations/1/workers

That nugget occurred to me and then I went and re-read the post.

For dumb mistakes like mine, a 400 error would have been much more useful.

Upvotes: 3

Related Questions