Spring数据REST:使用适当的HTTP方法更新资源的关联

Dan*_*iel 10 spring-data-rest

我正在使用Spring Data REST,我正在尝试使用Spring REST更改多对一关系,但我无法获得正确的http调用.

我的实体看起来像这样(像POST等创建的基本调用工作正常):

{
  "id" : 70,
  "productId" : yyy,
  "productdiscount" : 10,
  "version" : 0,
  "description" : "xxx",
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/rest/rules/70"
    },
    "timedefinition" : {
      "href" : "http://localhost:8080/rest/rules/70/timedefinition"
    }
  }
}

我想将ID为1的当前时间定义更改为ID 2.

我尝试了很多不同的错误调用类型.以下电话无效.

 curl -X PUT -H "Content-Type: text/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 "Content-Type: application/json" -d '{"timedefinition": {"href" : "http://localhost:8080/rest/timedefinition/0", "rel" : "timedefinition"} }' http://localhost:8080/rest/rules/70/timedefinition

我得到的错误是:

"message":"必须只发送一个链接来更新不是List或Map的属性引用."

http://docs.spring.io/spring-data/rest/docs/2.0.1.RELEASE/reference/html/上的主要参考资料不能很好地提供有关上述主题的大量信息.

有关正确的REST查询格式以更新实体关联的任何见解和解释都非常感谢!

Dan*_*iel 13

正确的答案就像我的评论:

curl -v -X PUT -H "Content-Type: text/uri-list" -d "http://localhost:8080/rest/timedefinition/0" http://localhost:8080/rest/rules/70/timedefinition
Run Code Online (Sandbox Code Playgroud)