Connect Java rest API to JIra

Peter Penzov picture Peter Penzov · Jul 28, 2016 · Viewed 12.8k times · Source

I want to connect Java with JIRA trial account. I tested this code:

public class JiraImpl
{
    private static URI JIRA_URL = URI.create("https://sonoratest.atlassian.net");
    private static final String JIRA_ADMIN_USERNAME = "[email protected]";
    private static final String JIRA_ADMIN_PASSWORD = "sonpass";

    public static void main(String[] args) throws IOException, InterruptedException, ExecutionException
    {
        try
        {
            AsynchronousJiraRestClientFactory factory = new AsynchronousJiraRestClientFactory();
            JiraRestClient restClient = factory.createWithBasicHttpAuthentication(JIRA_URL, JIRA_ADMIN_USERNAME, JIRA_ADMIN_PASSWORD);

            Iterable<BasicProject> allProjects = restClient.getProjectClient().getAllProjects().claim();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

But when I run it nothing happens. Wahat is the proper way to get data from JIRA using REST API?

Update. I also tried this:

private static URI JIRA_URL = URI.create("https://sonoratest.atlassian.net/rest/auth/1/session");

I get

java.util.concurrent.ExecutionException: RestClientException{statusCode=Optional.of(404), errorCollections=[ErrorCollection{status=404, errors={}, errorMessages=[]}]}
    at com.google.common.util.concurrent.AbstractFuture$Sync.getValue(AbstractFuture.java:299)
    at com.google.common.util.concurrent.AbstractFuture$Sync.get(AbstractFuture.java:286)
    at com.google.common.util.concurrent.AbstractFuture.get(AbstractFuture.java:116)
    at com.google.common.util.concurrent.ForwardingFuture.get(ForwardingFuture.java:63)
    at com.atlassian.jira.rest.client.internal.async.DelegatingPromise.get(DelegatingPromise.java:102)
    at com.jira.impl.JiraImpl.main(JiraImpl.java:23)
Caused by: RestClientException{statusCode=Optional.of(404), errorCollections=[ErrorCollection{status=404, errors={}, errorMessages=[]}]}

Answer

Somaiah Kumbera picture Somaiah Kumbera · Aug 4, 2016

Try getting an issue first, since that is so basic.

import java.net.URI;
import java.util.Optional;

import com.atlassian.jira.rest.client.api.JiraRestClient;
import com.atlassian.jira.rest.client.api.domain.Issue;
import com.atlassian.jira.rest.client.internal.async.AsynchronousJiraRestClientFactory;
import com.atlassian.util.concurrent.Promise;

public class JRC
{
    public Issue getIssue(String issueKey) throws Exception
    {
        final URI jiraServerUri = new URI("https://jira-domain");
        final JiraRestClient restClient = new AsynchronousJiraRestClientFactory().createWithBasicHttpAuthentication(jiraServerUri, "[email protected]", "password");
        Promise issuePromise = restClient.getIssueClient().getIssue(issueKey);
        return Optional.ofNullable((Issue) issuePromise.claim()).orElseThrow(() -> new Exception("No such issue"));
    }

}

You can also take a look at this code to get a fully working sample:

https://github.com/somaiah/jrjc