Sha*_*ood 5 java spring spring-data-rest
我无法弄清楚如何使用@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;
}
Run Code Online (Sandbox Code Playgroud)
我的存储库正在使用vanilla 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> {
}
Run Code Online (Sandbox Code Playgroud)
我已成功使用POST来创建单独的ParentEntity和ChildEntity,但我似乎无法弄清楚如何使用内置接口PUT/PATCH两者之间的关系.
看起来我应该能够使用PUT将JSON发送到类似的东西http://localhost:8080/api/parents/1/children,但到目前为止,我找不到一个有效的结构.
Sha*_*ood 10
I found an answer here: How to update reference object in Spring-data rest?
By using "Content-Type: text/uri-list" instead of JSON, it is possible to "add" a resource to the collection with a PUT and pass in the URI. You can remove the resource with a DELETE.
After some digging, I discovered that the Spring documentation does describe this: http://docs.spring.io/spring-data/rest/docs/2.2.0.RELEASE/reference/html/#repository-resources.association-resource.
| 归档时间: |
|
| 查看次数: |
5574 次 |
| 最近记录: |