[我正在使用Spring Data REST,并且我试图使用Spring REST更改多对一关系,但是我无法正常使用http调用。
我的实体看起来像这样(基本调用,例如用POST创建等,效果很好):
{“ id”:70,“ productId”:yyy,“ productdiscount”:10,“版本”:0,“ description”:“ xxx”,“ _links”:{“自我”:{“ href”:“ http:// localhost:8080 / rest / rules / 70”},“ timedefinition”:{“ href”:“ http:// localhost:8080 / rest / rules / 70 / timedefinition”}}}
我想将具有ID 1的当前时间定义更改为ID 2。
我尝试了很多不同的呼叫类型,但有不同的错误。以下通话无效。
curl -X PUT -H“内容类型:文本/ uri-list” -d'http:// localhost:8080 / rest / timedefinition / 1'http:// localhost:8080 / rest / rules / 70 / timedefinition
收到以下错误:
无法将java.lang.String类型的值转换为domain.TimeDefinition的值'0';嵌套异常是java.lang.IllegalArgumentException:为类domain.TimeDefinition提供了错误类型的ID。预期:类java.lang.Integer,得到类java.lang.Long
另一个例子是这样:
curl -X PUT -H“内容类型:application / json” -d'{“ timedefinition”:{“ href”:“ http:// localhost:8080 / rest / timedefinition / 0”,“ rel”:“ timedefinition “}}'http:// localhost:8080 / rest / rules / 70 / timedefinition
我得到的错误是:
“ message”:“必须仅发送1个链接来更新不是列表或地图的属性引用。”
http://docs.spring.io/spring-data/rest/docs/2.0.1.RELEASE/reference/html/上的主要参考文献并没有提供太多有关上述主题的信息。
非常感谢您对用于更新实体关联的正确REST查询格式的任何见解和解释!
正确的答案是我的评论:
curl -v -X PUT -H "Content-Type: text/uri-list" -d "http://localhost:8080/rest/timedefinition/0" http://localhost:8080/rest/rules/70/timedefinition
spring-data-rest-webmvc:3.2.0-RELEASE
中存在一个错误,每次发送PUT以创建一对一关联时,该错误都会引发此错误。
将spring-boot-starter-parent从2.2.0.RELEASE
更改为2.2.1.RELEASE
为我解决了此问题。您也可以手动覆盖spring-data-rest-webmvc
至3.2.1.RELEASE
。
相关提交:https://github.com/spring-projects/spring-data-rest/commit/202a8aa30221b81dece183b74d81278deaf45321
您还可以通过将关系嵌入到[[_links节点中来使用application / json内容类型。
curl -X PUT -H "Content-Type: application/json" -d '{"_links":{"timedefinition": {"href" : "http://localhost:8080/rest/timedefinition/0", "rel" : "timedefinition"} }}' http://localhost:8080/rest/rules/70/timedefinition