示例:课程和教师有多对一的关系,如何通过Spring-data rest改变某个课程的教师?
GET http://localhost:7070/study-spring-data/course/2
Run Code Online (Sandbox Code Playgroud)
响应:
{
"name" : "CSCI-338 Hardcore Java",
"_links" : [ {
"rel" : "course.Course.teacher",
"href" : "http://localhost:7070/study-spring-data/course/2/teacher"
}, {
"rel" : "self",
"href" : "http://localhost:7070/study-spring-data/course/2"
} ]
}
GET http://localhost:7070/study-spring-data/course/2/teacher
Run Code Online (Sandbox Code Playgroud)
响应:
{
"_links" : [ {
"rel" : "course.Course.teacher",
"href" : "http://localhost:7070/study-spring-data/course/2/teacher/1"
} ]
}
Run Code Online (Sandbox Code Playgroud)
如上所示,课程2与教师1相关,如何将教师改为教师2?
我试过了:
成功更新课程名称:
PUT http://localhost:7070/study-spring-data/course/2
有效载荷
{
"name" : "CSCI-223 Hardcore C++",
}
Run Code Online (Sandbox Code Playgroud)
尝试更新参考对象教师时失败:
PUT http://localhost:7070/study-spring-data/course/2/teacher
Run Code Online (Sandbox Code Playgroud)
有效载荷
{
"_links" : [ {
"rel" : "course.Course.teacher",
"href" : "http://localhost:7070/study-spring-data/course/2/teacher/2" …Run Code Online (Sandbox Code Playgroud) 在我的项目中,我有两个域模型.父母和子实体.父级引用子权利列表.(例如,帖子和评论)两个实体都有它们的弹簧数据JPA CrudRepository<Long, ModelClass>接口,它们被公开为@RepositoryRestResource
HTTP GET和PUT操作正常工作,并返回这些模型的漂亮HATEOS表示.
现在我需要一个特殊的REST端点"创建一个引用一个或多个现有子实体的新Parent ".我想将对子项的引用作为text/uri-list发布,我在请求正文中传递,如下所示:
POST http://localhost:8080/api/v1/createNewParent
HEADER
Content-Type: text/uri-list
HTTP REQUEST BODY:
http://localhost:8080/api/v1/Child/4711
http://localhost:8080/api/v1/Child/4712
http://localhost:8080/api/v1/Child/4713
Run Code Online (Sandbox Code Playgroud)
如何实现此休止端点?这是我到目前为止所尝试的:
@Autowired
ParentRepo parentRepo // Spring Data JPA repository for "parent" entity
@RequestMapping(value = "/createNewParent", method = RequestMethod.POST)
public @ResponseBody String createNewParentWithChildren(
@RequestBody Resources<ChildModel> childList,
PersistentEntityResourceAssembler resourceAssembler
)
{
Collection<ChildModel> childrenObjects = childList.getContent()
// Ok, this gives me the URIs I've posted
List<Link> links = proposalResource.getLinks();
// But now how to convert these URIs to domain objects???
List<ChildModel> …Run Code Online (Sandbox Code Playgroud)