9

示例:类 Course 和 Teacher 具有多对一关系,如何通过 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"
      } ]
    }

谢谢!

4

3 回答 3

14

像这样的东西怎么样:

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 book 建议的一种方式。

于 2013-07-31T21:30:04.587 回答
0

我认为您应该检查 Cascading for Course。因为如果您正在更新课程,那么它也应该更新您的老师,如果要为您的课程分配级联更新或全部。

于 2013-08-01T04:58:42.900 回答
0

使用httpie PATCH 而不是 PUT 对我有用:

http PATCH :7070/study-spring-data/course/2 teacher="http://localhost:7070/study-spring-data/teacher/2"

同样,这不适用于 PUT。

于 2017-05-05T12:31:55.520 回答