2016-07-28 116 views
0

我想用JIRA試用帳戶連接Java。我測試了這個代碼:將Java rest API連接到JIra

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

但是當我運行它時什麼也沒有發生。 Wahat是使用REST API從JIRA獲取數據的正確方法?

更新。我也試過這樣:

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

我得到

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=[]}]} 
+0

你得到一個錯誤? –

+1

那麼,這段代碼實際上不會產生任何輸出,所以除非你遇到錯誤,否則我沒有看到問題 – BoDidely

+0

我更新了這篇文章。 –

回答

2

先嚐試得到一個問題,因爲這是如此基本的。

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

} 

您也可以看看這個代碼,以獲得充分的工作樣本:

https://github.com/somaiah/jrjc