It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

java + rest + jql

I want to fetch based on jql query.

My code:

final URI jiraServerUri = new URI(Constants.JIRA_SEARCH_URL);
		final JiraRestClient restClient = new AsynchronousJiraRestClientFactory().createWithBasicHttpAuthentication(https://localhost:8080/jira/rest/api/latest/search, "xxx", "xxx"); // It throws 403

How do i fetch data acording to jql

jql = "reporter=somename";

2 answers

Hi,

A example with JRJC 2.00-m25:

 

import java.io.IOException;
import java.net.URI;

import com.atlassian.jira.rest.client.api.IssueRestClient;
import com.atlassian.jira.rest.client.api.JiraRestClient;
import com.atlassian.jira.rest.client.api.JiraRestClientFactory;
import com.atlassian.jira.rest.client.api.SearchRestClient;
import com.atlassian.jira.rest.client.api.domain.Issue;
import com.atlassian.jira.rest.client.api.domain.SearchResult;
import com.atlassian.jira.rest.client.internal.async.AsynchronousJiraRestClientFactory;

public class JiraRestClientExample2 {

public static void main(String[] args) {
System.out.println("START");
final String jiraUser = "user";
final String jiraPassword = "pass";
final String jiraBaseURL = "http://localhost:8080";
URI jiraServerUri = null;
JiraRestClient client = null;
try{
jiraServerUri = new URI(jiraBaseURL);
JiraRestClientFactory factory = new AsynchronousJiraRestClientFactory();
client = factory.createWithBasicHttpAuthentication(jiraServerUri, jiraUser, jiraPassword);
IssueRestClient issueClient = client.getIssueClient();
SearchRestClient searchClient = client.getSearchClient();
//CONSTRUIMOS LA PETICION

SearchResult result = searchClient.searchJql("reporter=somename").claim();

for(Issue issue : result.getIssues()){

System.out.println(issue.getKey());

}


}catch(Exception e){
e.printStackTrace();
}finally{
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("END");
}

}

nic Community Leader Feb 22, 2017

I think Azhar already has all of that.  The problem is now that it does not return what is needed, the client is converting the (desired) JQL to an unwanted format.

0 votes
nic Community Leader Feb 19, 2017

There's some docs on the REST API, but I find the examples in there a better starting point than the basic docs.  Have a look at https://developer.atlassian.com/jiradev/jira-apis/jira-rest-apis/jira-rest-api-tutorials/jira-rest-api-example-query-issues - it tells you how to form a number of query strings for use with the REST API

Hi Nic,

Thank you for your response. Finally my authentication is working. But the problem is i have too much code written already using http client which gives me data in json format. So i want the same using rest + java. I want Json result when i hit the query. Currently it gives me the whole list of scrips etc. Its is not in json format.

Can you please help me how to retrieve in json format. Thanks in advance.

Promise<SearchResult> searchJqlPromise = restClient.getSearchClient().searchJql(SRE_QUERY);
            for (Issue issue : searchJqlPromise.claim().getIssues()) {
                System.out.println(issue); //It doesnt gives the output in json format. Can i know how do i get in that format
            }
nic Community Leader Feb 21, 2017

Ok, you should dump whatever your "restClient" class is because it's reformatting valid json for you.

JIRA responds to REST calls with json data.  Just use it directly instead of using whatever that client is.

Hi Nic,

It doesnt give me json data. It gives only some random object

final JiraRestClientFactory  factory = new AsynchronousJiraRestClientFactory();
		final JiraRestClient restClient = factory.createWithBasicHttpAuthentication(new URI("https://xxx.networkname.com/rest/api/latest/search?jql="+SRE_QUERY)
System.out.println("========="+restClient); // it prints =========com.atlassian.jira.rest.client.internal.async.AsynchronousJiraRestClient@6d7b4f4c
nic Community Leader Feb 21, 2017

Ah, my last comment was unclear, sorry.

When you make a REST call to JIRA, you get JSON back from it.  The problem you are having here is your rest client code  is  reformatting the json that JIRA gives to it.

Stop using the rest client, and use a simple plain REST call directly to JIRA.  That will give you json to work with.

How do i do that is there any example?

nic Community Leader Feb 27, 2017

You should use whatever REST call/library your chosen language supports.

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted in United States

Welcome new members!

Wow, I got swamped and now that I've finally gotten my head back above water, I find we have several new members! Welcome @Darren Keith lowman , @Amanda Babb , @Luis Zapata , @Joli Holmes , and @bh...

28 views 0 2
View post

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you