【发布时间】:2016-08-27 22:18:27
【问题描述】:
我的情况: 我有一个可以有很多工人的组织。我试图在 SDR 项目的主要撰稿人 this example 之后向一个组织添加一个新工作人员,但我遇到了各种错误(包括 204,但没有任何反应)。
这是我的实体和休息电话:
@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;
}
{
"_links": {
"workers": {
"href": "http://localhost:8080/hal/workers{?page,size,sort}",
"templated": true
}
}
}
发布http://localhost:8080/hal/workers
{
"givenName": "James",
"familyName": "Bond",
"dob": "1970-01-01",
"nationalId": "XXX-60-XXXX",
"photo": null,
}
回复:
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"
}
}
}
最后一步,根据描述中的链接:
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!"
}
做一些调试很明显具体的抱怨是什么。堆栈跟踪指向这里:
@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() -> 组织
isInstance(bean) -> org.springframework.hateoas.Resource 的 bean 实例
对此有何意见?我按照信中的指示进行了操作。
【问题讨论】:
标签: spring-data spring-data-jpa spring-data-rest