如何在Spring-data rest中更新引用对象?

Liu*_*Liu 9 java rest spring spring-data spring-data-rest

示例:课程和教师有多对一的关系,如何通过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)

谢谢!

Wir*_*rus 13

这样的事情怎么样:

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
Run Code Online (Sandbox Code Playgroud)

这是O'Reilly的Spring Data书中提出的一种方式.