2014-03-19 22 views
6

我正在使用Spring Data REST,並且試圖使用Spring REST更改多對一關係,但我無法獲得正確的http調用工作。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" 
    } 
    } 
} 

我想改變當前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 

我得到的錯誤是:

「消息」:「必須發送只有1個鏈接來更新不是List或Map的屬性引用。「

http://docs.spring.io/spring-data/rest/docs/2.0.1.RELEASE/reference/html/的主要參考文獻沒有提供關於上述不幸事件的更多信息。

任何有關適當的REST查詢格式來更新實體關聯的任何見解和解釋都將不勝感激!

+0

好了,所以這個查詢的實際工作:'捲曲-v -X PUT -H 「內容類型:文本/ URI列表」 -d「HTTP://本地主機:8080/REST/timedefinition/0 「http:// localhost:8080/rest/rules/70/timedefinition」。但是我必須將時間定義中的ID從int更改爲long。這可能是春季數據REST中的一個錯誤? – Daniel

回答

10

正確答案是我的評論:

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

您也可以通過嵌入你的關係到_links節點使用的應用程序/ 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