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



我有Parent和Nested对象。我可以使用GET /parent/{parentId}/nested获得嵌套的列表,但我不能成功地对其进行POST/PUT。在POST结果中,我得到了HTTP204,并且父对象有空的嵌套对象列表。

在日志中,我看到Hibernate并没有在DB中插入任何内容。在调试过程中,我看到用于取消实现的Jackson对象映射器将"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 datarest文档中有一个"关联资源"部分,它告诉您在这个资源上有什么可能。创造一个新的孩子并将其联系起来是不可能的。

我不知道为什么会这样。

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

最新更新