我无法准确地弄清楚如何使用 @RepositoryRestResource 接口在两个相当简单的实体之间创建多对多关系。
例如,我有一个简单的父子实体关系,如下所示:
@Entity
public class ParentEntity {
@Id
@GeneratedValue
private Long id;
@ManyToMany
private List<ChildEntity> children;
}
@Entity
public class ChildEntity {
@Id
@GeneratedValue
private Long id;
@ManyToMany(mappedBy="children")
private List<ParentEntity> parents;
}
我的存储库使用的是普通 Spring @RepositoryRestResource HATEOS API:
@RepositoryRestResource(collectionResourceRel = "parents", path = "parents")
public interface ParentRepository extends PagingAndSortingRepository<ParentEntity, Long> {
}
@RepositoryRestResource(collectionResourceRel = "children", path = "children")
public interface ChildRepository extends PagingAndSortingRepository<ChildEntity, Long> {
}
我已经成功地使用 POST 创建了单独的 ParentEntity 和 ChildEntity,但我似乎无法弄清楚如何使用内置接口来 PUT/PATCH 两者之间的关系。
看来我应该能够使用 PUT 将 JSON 发送到类似的东西http://localhost:8080/api/parents/1/children
,但到目前为止我还没有找到有效的结构。
我在这里找到了答案:如何在Spring-data Rest中更新引用对象?
通过使用“Content-Type: text/uri-list”而不是 JSON,可以使用 PUT 将资源“添加”到集合中并传入 URI。您可以使用 DELETE 删除资源。
经过一番挖掘,我发现 Spring 文档确实描述了这一点:http://docs.spring.io/spring-data/rest/docs/2.2.0.RELEASE/reference/html/#repository-resources.association-resource.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)