简体   繁体   English

Java PUT 请求 Zotero 400 错误请求

[英]Java PUT Request Zotero 400 Bad Request

I am trying to send PUT request to the Zotero API, but I keep getting an error:我正在尝试向 Zotero API 发送 PUT 请求,但我不断收到错误消息:

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

The JSON being sent is fine, so it is something with my code.发送的 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");
        }
    }

The method above then calls the method below上面的方法然后调用下面的方法

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);
        }

Not really sure what is wrong.不太确定出了什么问题。 I'd appreciate any help - zoter-dev said that the PUT request should work and it's something with my code.我会很感激任何帮助 - zoter-dev 说 PUT 请求应该可以工作,这与我的代码有关。 Thanks!谢谢!

I'd suggest you take a good look at the Zotero Web API documentation .我建议你好好看看Zotero Web API 文档

If you examine the creating an item section you'll find what you need to pass in your API call in order for it to work:如果您检查创建项目部分,您会发现需要在 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"
    }
  }
]

It's stated that All properties other than itemType, tags, collections, and relations are optional , meaning itemType is mandatory.声明All properties other than itemType, tags, collections, and relations are optional ,这意味着itemType是必需的。 You must fill in these four properties, at least, if you want your call to succeed.如果您希望调用成功,至少必须填写这四个属性。

If you don't have any data for tags , collections or relations you could just pass empty property values:如果您没有tagscollectionsrelations的任何数据,您可以只传递空属性值:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM