How to update reference object in Spring-data rest?(如何更新 Spring-data rest 中的引用对象?)
问题描述
示例: class Course 和 Teacher 多对一关系,如何通过 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
带有效载荷
{
"name" : "CSCI-223 Hardcore C++",
}
不成功尝试更新参考对象教师时:
unsuccessful when try to update reference object teacher:
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 book 建议的一种方式.
This is a way suggested by O'Reilly's Spring Data book.
这篇关于如何更新 Spring-data rest 中的引用对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何更新 Spring-data rest 中的引用对象?


基础教程推荐
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 降序排序:Java Map 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01