Jira Cloud integration with Rest API using JAVA

How can we do create, read, update and delete(CRUD) operation on the issues in Jira cloud like we can do it in servers? In server, there are lots of packages and many classes which helps us to interact with Jira. Example To do the CRUD operations on an issue we can make use of API like “import com.atlassian.jira.rest.client.api” connect (create a RestClient and IssueRestClient) to Jira server to get an issue object. Do we any repo’s or packages which helps us to interact with Jira cloud as we can do with Jira server.

Pretty much you can do all CURD Operation with help of REST API provided by cloud.

https://developer.atlassian.com/cloud/jira/platform/rest/v2/

Hi Umang,

Thank you for the response.

They have used JSON in java for all the operations and i’m not very familiar with JSON is there any way we can just make use of just JAVA.

Ultimately the REST API expects JSON so any Java API would ultimately produce JSON as an output. Instead of just dumping a plain string we thought we would go the extra mile and show the example using the Jackson so that it was “Java native”. For example:

// The payload definition using the Jackson library
JsonNodeFactory jnf = JsonNodeFactory.instance;
ObjectNode payload = jnf.objectNode();
{
  root.put("name", "new name");
}

// Connect Jackson ObjectMapper to Unirest
Unirest.setObjectMapper(new ObjectMapper() {
   private com.fasterxml.jackson.databind.ObjectMapper jacksonObjectMapper
           = new com.fasterxml.jackson.databind.ObjectMapper();

   public <T> T readValue(String value, Class<T> valueType) {
       try {
           return jacksonObjectMapper.readValue(value, valueType);
       } catch (IOException e) {
           throw new RuntimeException(e);
       }
   }

   public String writeValue(Object value) {
       try {
           return jacksonObjectMapper.writeValueAsString(value);
       } catch (JsonProcessingException e) {
           throw new RuntimeException(e);
       }
   }
});

// This code sample uses the  'Unirest' library:
// http://unirest.io/java.html
HttpResponse<JsonNode> response = Unirest.put("https://your-domain.atlassian.net/rest/agile/1.0/sprint/{sprintId}")
  .basicAuth("email@example.com", "<api_token>")
  .header("Accept", "application/json")
  .header("Content-Type", "application/json")
  .body(payload)
  .asJson();

System.out.println(response.getBody());

What could we do to make it easier to use and understand?

Hi rmassaioli,

Thank you for your response.
I did understand the code and I still have an issue in this on the 5th line when you mention “root.put(“name”, “new name”);” I can’t understand what is ‘root’ and you have not initialized. can you help me to understand the variable ‘root’.

HI @manjunathr,

Reading between the lines, I guess root here is like the root node of your json payload, ie in json it would look like this:

{
    'name': 'new name'
}

Cheers,
Anne

1 Like

Oops, the name root should actually be payload. I’m fixing the auto-generated documentation now to make this be the case for all of the examples in the documentation.

But what Anne said is correct, it is the root node of the JSON payload.

1 Like