Search code examples
javajsonspring-bootbitbucketbitbucket-api

Retrieve Text file content using Bitbucket server rest APIs in Java JsonNode format


I am trying to use the bitbucket server rest API to fetch file content(.json file) from a bitbucket repository in a Java spring boot Application but the response is not as expected.

Content in the file meta.json

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

API used https://bitbucket.domain.com/rest/api/1.0/projects/my-project/repos/my-repo/browse/path/to/meta.json

Java snippet used to get file content

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

Actual output:

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

Whereas the output I want is the following

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

So I can convert it to JsonNode and use it.

Observation

While hitting the repo url with ?raw param (https://bitbucket.domain.com/projects/my-project/repos/my-repo/browse/path/to/meta.json?raw) I was getting the excepted output in text format, not json format. I also tried using the same url in my spring boot application but that didnt work either.

Any suggestion on how can I get the expected output?


Solution

  • I was able fix this issue, by using Unirest

    The following syntax was used

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