4

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.

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This works. I was in a similar situation. However, this keeps updating (only)one resource. Do you know how to add a new resource? For instance, add more than one worker to the organisation.
You can't - that's not how it works. To add an additional resource you need to GET /hal/organizations/1/workers which yields an array that you add to and then PUT or POST to the same resource. I think semantically a PATCH would be the right step but I know for a fact that PATCH is supported the same way the other verbs are.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.