问题描述
这样的事情怎么样:
curl -v -X PUT -H "Content-Type: text/uri-list" \
-d "http://localhost:7070/study-spring-data/teacher/1" \
http://localhost:7070/study-spring-data/course/123/teacher
这是O’Reilly的Spring Data书中建议的方法。
解决方法
示例: 班级课程和教师之间存在多对一的关系,如何通过Spring-data rest改变某个课程的教师?
GET http://localhost:7070/study-spring-data/course/2
响应:
{
"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
响应:
{
"_links" : [ {
"rel" : "course.Course.teacher","href" : "http://localhost:7070/study-spring-data/course/2/teacher/1"
} ]
}
如上所示,课程2与老师1相关联,如何将老师更改为老师2?
我努力了:
成功更新课程名称:
PUT http://localhost:7070/study-spring-data/course/2
带有有效载荷
{
"name" : "CSCI-223 Hardcore C++",}
*尝试更新参考对象教师时 *失败 :
PUT http://localhost:7070/study-spring-data/course/2/teacher
带有有效载荷
{
"_links" : [ {
"rel" : "course.Course.teacher","href" : "http://localhost:7070/study-spring-data/course/2/teacher/2"
} ]
}
谢谢!