Java PUT 请求 Zotero 400 错误请求

时间:2021-03-20 21:50:21

标签: java put

我正在尝试向 Zotero API 发送 PUT 请求,但我一直收到错误消息:

Caused by: org.springframework.web.client.HttpClientErrorException$BadRequest: 400 Bad Request: ['itemType' property not provided]

发送的 JSON 没问题,所以我的代码有问题。

private void handleUpdateItemButton(ActionEvent event) throws IOException {
        Properties props = restConnection.getAccessProperties();
        ResponseEntity<JsonNode> res = restConnection.getRestTemplate().exchange(this.getItem(props, itemKey), new ParameterizedTypeReference<JsonNode>() {
        });

        if (res.getStatusCode() == HttpStatus.OK) {
            JsonNode jsonNode = res.getBody();
            printJSON(jsonNode);
            JSONObject jsonObject = convertNodetoObject(jsonNode);
            JSONObject jsonData = jsonObject.getJSONObject("data");
            //jsonObject.getJSONObject("data").put("title", "This is the new title");
            jsonData.put("title", "This is the new title");

            ResponseEntity<JsonNode> updatedItem = restConnection.getRestTemplate().exchange(this.updateItem(props, jsonData, itemKey), new ParameterizedTypeReference<JsonNode>() {
            });

        }

        else{
            System.out.println("This item cannot be updated");
        }
    }

上面的方法然后调用下面的方法

private RequestEntity updateItem(Properties props, JSONObject item, String itemKey) throws JsonProcessingException {
            ResponseEntity<JsonNode> res = restConnection.getRestTemplate().exchange(this.getItem(props, itemKey), new ParameterizedTypeReference<JsonNode>() {
            });        
        return RequestEntity
                .put(restConnection.getZoteroBaseURL() + "/users/" + props.getProperty("username") + "/items/" + itemKey)
                .header("Zotero-API-Version", "3")
                .header("Zotero-API-Key", props.getProperty("key"))
                .header("If-Unmodified-Since-Version", numberBody.get("version").toString())
                .header("Content-Type", "application/json")
                .body(item);
        }

不太确定哪里出了问题。我很感激任何帮助 - zoter-dev 说 PUT 请求应该可以工作,这与我的代码有关。谢谢!

1 个答案:

答案 0 :(得分:0)

我建议你好好看看 Zotero Web API documentation

如果您检查 creating an item 部分,您会发现您需要在 API 调用中传递什么才能使其工作:

[
  {
    "itemType" : "book",
    "title" : "My Book",
    "creators" : [
      {
        "creatorType":"author",
        "firstName" : "Sam",
        "lastName" : "McAuthor"
      },
      {
        "creatorType":"editor",
        "name" : "John T. Singlefield"
      }
    ],
    "tags" : [
      { "tag" : "awesome" },
      { "tag" : "rad", "type" : 1 }
    ],
    "collections" : [
      "BCDE3456", "CDEF4567"
    ],
    "relations" : {
      "owl:sameAs" : "http://zotero.org/groups/1/items/JKLM6543",
      "dc:relation" : "http://zotero.org/groups/1/items/PQRS6789",
      "dc:replaces" : "http://zotero.org/users/1/items/BCDE5432"
    }
  }
]

据说 All properties other than itemType, tags, collections, and relations are optional,意思是 itemType 是必须的。 如果您希望调用成功,您至少必须填写这四个属性。

如果您没有 tagscollectionsrelations 的任何数据,您可以只传递空属性值:

{
  "itemType" : "note",
  "note" : "My sample note",
  "tags" : [],
  "collections" : [],
  "relations" : {}
}