在 Spring 中,可以将嵌套的 @OneToMany 对象添加到 POST /parent/{parentId}/nested?

In Spring data rest is possible to add nested @OneToMany object to parent over POST /parent/{parentId}/nested?

我有父对象和嵌套对象。我可以使用 GET /parent/{parentId}/nested 获取嵌套列表,但我无法 POST/PUT 成功。在 POST 结果上,我得到 HTTP 204 并且父对象的嵌套对象列表为空。

在日志中我看到 Hibernate 没有在数据库中插入任何东西。 在调试过程中,我看到用于反序列化的 Jackson Object Mapper 将“org.springframework.hateoas.Resources<java.lang.Object>”作为 JavaType。

这是我的 POST 的真实示例: curl 'http://localhost:8000/api/users/402880e554b8f7960154b8f7adbc0000/orders/' -i -X POST -H 'Authorization: Basic OLOLO==' -H 'Accept: application/json' -H 'deviceCurrencyCode: USD' -H 'Content-Type: application/json; charset=UTF-8' -d '{"orderPrice": 0,"tax": 0,"shippingCost": 0,"credits": 0,"addresses": [{"firstName": "Figli","lastName": "Migli","zipCode": "5555","city": "Riga","country": "Tlmltr","phone": "7777","address": "Adddr"}],"status": "status"}'

我知道可以做到 POST /nested '{"parent":"/link/to/parent"}'。但是,如果这是通过父对象获取嵌套对象的方法,为什么我不能以这种方式 POST/PUT/PATCH 它们?我查了一下,是不是 RESTfull,好像是。

spring 数据其余文档中有一节 "The association resource" 告诉您此资源的可能性。并且不可能创建新的 child 并将其关联。

我不知道这是为什么。

获得此行为的唯一方法(我知道)是实现一个自定义控制器来为您执行此操作。 (确保您没有在自定义控制器的 class 级别上使用 @RequestMapping 注释,以免覆盖比您想要的更多 spring 数据休息功能)