【问题标题】:Retrieve Text file content using Bitbucket server rest APIs in Java JsonNode format使用 Java JsonNode 格式的 Bitbucket 服务器 rest API 检索文本文件内容
【发布时间】:2023-02-10 16:00:30
【问题描述】:

我正在尝试使用 bitbucket 服务器 rest API 从 Java spring boot 应用程序中的 bitbucket 存储库中获取文件内容(.json 文件),但响应与预期不同。

文件中的内容元.json

{
  "key1": "value1",
  "key2": "value2"
}

使用的APIhttps://bitbucket.domain.com/rest/api/1.0/projects/my-project/repos/my-repo/browse/path/to/meta.json

java sn -p 用于获取文件内容

String url = "https://bitbucket.domain.com/rest/api/1.0/projects/my-project/repos/my-repo/browse/path/to/meta.json";
JsonNode bucketData = restTemplate.exchange(url, HttpMethod.GET, entity, JsonNode.class).getBody();

实际输出:

{
    "lines": [
        {
            "text": "{"
        },
        {
            "text": "  \"key1\": \"value1\","
        },
        {
            "text": "  \"key2\": \"value2\""
        },
        {
            "text": "}"
        }
    ],
    "start": 0,
    "size": 4,
    "isLastPage": true
}

而我想要的输出如下

{
  "key1": "value1",
  "key2": "value2"
}

所以我可以将它转换为 JsonNode 并使用它。

观察

在点击回购网址时?生的param (https://bitbucket.domain.com/projects/my-project/repos/my-repo/browse/path/to/meta.json?raw) 我得到的是文本格式的预期输出,而不是 json 格式。我还尝试在我的 spring boot 应用程序中使用相同的 url,但这也没有用。

关于如何获得预期输出的任何建议?

【问题讨论】:

    标签: java json spring-boot bitbucket bitbucket-api


    【解决方案1】:

    我能够通过使用Unirest 解决这个问题

    使用了以下语法

    String url = "https://bitbucket.domain.com/rest/api/1.0/projects/my-project/repos/my-repo/browse/path/to/meta.json";
    HttpResponse<T> response = Unirest.get(url).headers(headers).asObject(responseClass);
    JsonNode bucketData = response.getBody();
    

    【讨论】:

      猜你喜欢
      • 2014-04-27
      • 2012-06-24
      • 2023-01-27
      • 2017-03-24
      • 2011-05-31
      • 1970-01-01
      • 2017-11-30
      • 1970-01-01
      • 2021-10-06
      相关资源
      最近更新 更多