【问题标题】:Spring data REST: Update a resource´s association using proper HTTP methodSpring data REST:使用适当的 HTTP 方法更新资源的关联
【发布时间】:2014-04-25 08:11:25
【问题描述】:

我正在使用 Spring Data REST,我正在尝试使用 Spring REST 更改多对一关系,但我无法让正确的 http 调用正常工作。

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

{ “身份证”:70, “productId”:年年, “产品折扣”:10, “版本”:0, “描述”:“xxx”, “_链接”:{ “自己” : { “href”:“http://localhost:8080/rest/rules/70” }, “时间定义”:{ “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 for value '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":"必须只发送 1 个链接来更新不是 List 或 Map 的属性引用。"

不幸的是,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。但我不得不将时间定义中的 ID 从 int 更改为 long。这可能是 Spring Data REST 中的错误吗?

标签: spring-data-rest


【解决方案1】:

正确答案是我的评论:

curl -v -X PUT -H "Content-Type: text/uri-list" -d "http://localhost:8080/rest/timedefinition/0" http://localhost:8080/rest/rules/70/timedefinition

【讨论】:

    【解决方案2】:

    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

    【讨论】:

      【解决方案3】:

      您还可以通过将您的关系嵌入到 _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
      

      【讨论】:

        猜你喜欢
        • 2017-04-22
        • 2018-09-04
        • 2014-10-08
        • 2020-01-15
        • 1970-01-01
        • 2016-09-01
        • 2015-09-09
        • 1970-01-01
        • 2023-04-09
        相关资源
        最近更新 更多