在Spring数据中,可以通过POST / parent / {parentId} / nested将嵌套的@OneToMany对象添加到父级吗?

时间:2016-05-17 14:13:39

标签: rest one-to-many spring-data-rest spring-web

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

在日志中我看到Hibernate不会在DB中插入任何内容。 在调试期间,我看到用于deserealization的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"}'。但是,如果它是通过父级GET嵌套对象的方式,为什么我不能以这种方式POST / PUT / PATCH它们?我查了一下,是RESTfull,似乎是。

1 个答案:

答案 0 :(得分:5)

spring data rest文档中有一个"The association resource"部分,告诉您该资源的可能性。创造一个新的孩子并将其联系起来是不可能的。

我不知道为什么会这样。

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