如何在Spring-data rest中更新引用对象? [英] How to update reference object in Spring-data rest?

查看:372
本文介绍了如何在Spring-data rest中更新引用对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例:课程课程和教师有多对一的关系,如何通过Spring-data rest来改变某个课程的老师?

Example: class Course and Teacher having many-to-one relationship, how to change teacher for a certain course via 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?

As above shown, course 2 is associated with teacher 1, how to change teacher to teacher 2?

我试过:

成功更新了课程名称:

PUT http:// localhost:7070 / study-spring-data / course / 2
有效载荷

PUT http://localhost:7070/study-spring-data/course/2 with payload

    {
      "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"
      } ]
    }

谢谢!

推荐答案

这样的事情怎么样:

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

This is a way suggested by O'Reilly's Spring Data book.

这篇关于如何在Spring-data rest中更新引用对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆