2017-03-02 4 views
1

나는 세 가지 요소를 가지고 (사용자) 및 태그 목록이 있습니다.POST "인라인"하위 ressource 협회

프리젠 테이션을 만들거나 업데이트하는 경우 리소스 사용자와 태그를 연결해야합니다. Spring Data Rest - Association Resource의 설명서에 따르면 Content-Type: text/uri-list 헤더를 사용하여 할당 할 수 있습니다. 그러나이 경우 내가 하나가, 같은 태그에 대한 저자을 설정하는 하나의 또 다른, 프리젠 테이션을 만들려면 여러 번 호출 할 필요가 :

curl -i -X POST -H "Content-Type: application/json" -d '{"name": "P1"}' http://localhost:8080/api/presentations 

curl -i -X PATCH -H "Content-Type: text/uri-list" -d " 
http://localhost:8080/api/users/1" http://localhost:8080/api/presentations/1/author 

curl -i -X PATCH -H "Content-Type: text/uri-list" -d " 
http://localhost:8080/api/tags/1 
http://localhost:8080/api/tags/2" http://localhost:8080/api/presentations/1/tags 

나는 저자에 대한 별도의 호출을 우회하는 방법을 발견 그리고 어떻게 그 프리젠 테이션 호출의 "인라인"(thanks to Chetan Kokil) :

// WORK: 
curl -i -X POST -H "Content-Type: application/json" -d '{"name": "P1", "author": "http://localhost:8080/api/users/1"}' http://localhost:8080/api/presentations 

그래서 나에게 추가로 전화를 저장하고 나의 이해에 따라이 ACID 원칙을 따른다.

태그 목록과 동일하게하고 싶습니다.

// DON'T WORK: 
curl -i -X POST -H "Content-Type: application/json" -d '{"name": "P1", "author": "http://localhost:8080/api/users/1", "tags":["http://localhost:8080/api/tags/1","http://localhost:8080/api/tags/2"]}' http://localhost:8080/api/presentations 

그리고 오류 메시지가 다음 무엇입니까 :

{ 
    "cause": { 
    "cause": null, 
    "message": "Can not construct instance of com.example.model.Tag: no String-argument constructor/factory method to deserialize from String value ('http://localhost:8080/api/tags/2')\n at [Source: N/A; line: -1, column: -1]" 
    }, 
    "message": "Could not read payload!; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.example.model.Tag: no String-argument constructor/factory method to deserialize from String value ('http://localhost:8080/api/tags/2')\n at [Source: N/A; line: -1, column: -1]" 
} 

내 질문에, 그것은 그 만들 그렇다면, 올바른 구문은 무엇을 할 수 그래서 다음 호출을 시도?

감사합니다. 내가 솔루션 나 자신을 발견 내 질문 :

를 작성하는 동안

답변

0

아마 다른 사람을 도움이 될 것입니다. 당신은 "인라인"목록/수집을 위해 협회의 자원을 설정하려면

curl -i -X PATCH -H "Content-Type: application/json" -d '{"name": "P1", "author": "http://localhost:8080/api/users/1", "tags":[["http://localhost:8080/api/tags/1","http://localhost:8080/api/tags/2"]]}' http://localhost:8080/api/presentations 

당신은 배열 내부의 URI를 추가해야한다

// WORK only for updating of a list that already contains at least one resource: 
[["a/b/c", "a/b/c"]] 

하나의 배열이 작동하지 않습니다.

// DON'T WORK: 
["a/b/c", "a/b/c"]` 

업데이트 : 관련된 몇 가지 태그가 이미있는 경우에만 작동처럼

것 같습니다. 즉, 이는 새로운 Presentation 인스턴스를 만드는 동안 작동하지 않습니다. 이 버그

Can not deserialize instance of com.example.model.Tag out of START_ARRAY token\n at [Source: N/A; line: -1, column: -1]" 

가 : 목록이 비어 있으면, 그것은 나에게 다음과 같은 오류를 준다? 아직 해결책을 찾고 있습니다.