Exception while trying to read an issue using jira-rest-java-client

Alex Paransky April 11, 2020

Dear Community,

I am using jira-rest-java-client to read an issue from JIRA Cloud

   <dependency>
      <groupId>com.atlassian.jira</groupId>
      <artifactId>jira-rest-java-client</artifactId>
<version>2.0.0-m2</version>
</dependency>

My code is fairly straight forward:

 

public static void main(String[] args) throws Exception
{
final JiraRestClientFactory
factory =
new AsynchronousJiraRestClientFactory();

final JiraRestClient
jiraRestClient =
factory
.createWithBasicHttpAuthentication(
new URI(Constants.JIRA_URL),
Constants.USERNAME,
Constants.TOKEN
);

final IssueRestClient
issueClient =
jiraRestClient.getIssueClient();

final Promise<com.atlassian.jira.rest.client.domain.Issue>
promiseIssue =
issueClient.getIssue("OTPL-5018");

final com.atlassian.jira.rest.client.domain.Issue
issue =
promiseIssue.claim();

System.out.println(issue);
}

The issue is retrieved, but during the parsing of the author, the code is trying to retrieve attribute "name" which does not appear to exist in the JSON object.  

 

@Nullable
public static BasicUser parseBasicUser(@Nullable JSONObject json) throws JSONException {
if (json == null) {
return null;
}
final String username = json.getString("name");
if (!json.has(JsonParseUtil.SELF_ATTR) && "Anonymous".equals(username)) {
return null; // insane representation for unassigned user - JRADEV-4262
}

// deleted user? BUG in REST API: JRA-30263
final URI selfUri = optSelfUri(json, BasicUser.INCOMPLETE_URI);
return new BasicUser(selfUri, username, json.optString("displayName", null));
}

Instead, my object contains a field called "displayName".

As a result, the code in the  jira-rest-java-client library fails with the following exception:

Caused by: org.codehaus.jettison.json.JSONException: JSONObject["name"] not found.
at org.codehaus.jettison.json.JSONObject.get(JSONObject.java:360)
at org.codehaus.jettison.json.JSONObject.getString(JSONObject.java:487)
at com.atlassian.jira.rest.client.internal.json.JsonParseUtil.parseBasicUser(JsonParseUtil.java:167)

Is there something that I am not doing correctly?  

Why does the object returned from JIRA Cloud does not include "name", but rather has "displayName" instead?  

More importantly, how do I fix this?

3 answers

1 accepted

1 vote
Answer accepted
Alex Paransky April 13, 2020

There are a plethora of different JIRA clients hosted at Atlassian Artifactory and one of the issues was to figure out which one to use.  From other googling around I found out that version 

 

<dependency>
<groupId>
com.atlassian.jira</groupId>
<artifactId>jira-rest-java-client-core</artifactId>
<version>5.2.1</version>
<dependency>

Corrects the issue that I was having.  In addition to this dependency, the following dependencies are also required:

<dependency>
<groupId>io.atlassian.util.concurrent</groupId>
<artifactId>atlassian-util-concurrent</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>com.atlassian.jira</groupId>
<artifactId>jira-rest-java-client-api</artifactId>
<version>5.2.1</version>
<dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.atlassian.jira</groupId>
<artifactId>jira-rest-java-client-app</artifactId>
<version>5.2.1</version>
<scope>runtime</scope>
</dependency>


After adding these dependencies, the error is gone and I am able to access JIRA Cloud REST API to complete the work I am doing.

DPKJ
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 13, 2020

Good work @Alex Paransky 

I think newer versions have updated response parser for user object.

1 vote
DPKJ
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 11, 2020

@Alex Paransky

To add to @Matt Doar  's answer,

This is know issue as of now with 'jira-rest-client' while using Jira Cloud and reason for this mishap is GDPR.

Jira's rest API's for Cloud have started returning 'accountId' for a user object in place of 'username' or 'email', and JSON parser is not update to take care of this change.

As a workaround you can use custom 'rest-client' (like uni-rest) for accessing Jira's rest api. Or you can extend existing library to reflect the change.

poonam narwal November 26, 2020

Hi @DPKJ , 

I am using the following dependency

  <dependency>
<groupId>com.atlassian.jira</groupId>
<artifactId>jira-rest-java-client</artifactId>
<version>1.0</version>

</dependency>

I am getting the following exception when I am creating an issue

at com.atlassian.jira.rest.client.internal.jersey.AbstractJerseyRestClient.invoke(AbstractJerseyRestClient.java:75)
at com.atlassian.jira.rest.client.internal.jersey.AbstractJerseyRestClient.impl(AbstractJerseyRestClient.java:164)
at com.atlassian.jira.rest.client.internal.jersey.AbstractJerseyRestClient.postAndParse(AbstractJerseyRestClient.java:152)
at com.atlassian.jira.rest.client.internal.jersey.JerseyIssueRestClient.createIssue(JerseyIssueRestClient.java:398)
at com.JrJcExample.JRJCExample.createIssue(JRJCExample.java:124)
at com.JrJcExample.JRJCExample.main(JRJCExample.java:35)
Caused by: javax.ws.rs.WebApplicationException: java.lang.Exception: Error parsing JSON object.
at com.sun.jersey.json.impl.provider.entity.JSONObjectProvider.readFrom(JSONObjectProvider.java:93)
at com.sun.jersey.json.impl.provider.entity.JSONObjectProvider$App.readFrom(JSONObjectProvider.java:65)
at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:552)
at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:505)
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:605)
at com.sun.jersey.api.client.WebResource.post(WebResource.java:235)
at com.atlassian.jira.rest.client.internal.jersey.AbstractJerseyRestClient.doHttpMethod(AbstractJerseyRestClient.java:179)
at com.atlassian.jira.rest.client.internal.jersey.AbstractJerseyRestClient.access$000(AbstractJerseyRestClient.java:43)
at com.atlassian.jira.rest.client.internal.jersey.AbstractJerseyRestClient$7.call(AbstractJerseyRestClient.java:170)
at com.atlassian.jira.rest.client.internal.jersey.AbstractJerseyRestClient.invoke(AbstractJerseyRestClient.java:54)

 

Can you help me out on this, if I am trying to use jrjc core dependency I couldn't able to accomplish OAuth 1.0 authentication. I am getting this exception when I am using to create issue in Jira server, I am not getting any exception with cloud jira.

DPKJ
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 29, 2020

Hey @poonam narwal sorry for the late response, I was on holidays.

Looking at error it seems you are not getting proper JSON.

Are you on Server or Cloud?

poonam narwal November 30, 2020

Hi @DPKJ  No problem, Yes I couldn't able to recieve a proper response from the server, It is creating an issue in server but I am getting garbled text as a response. I came to know this once I have tried by calling rest API through java code. Here is my code.

 

Client client = Client.create();
OAuthClientFilter filter = new OAuthClientFilter(client.getProviders(), oAuthParameters, oAuthSecrets);
client.addFilter(filter);
URI issueuri = new URI("<MY DOMAIN URL HERE>/rest/api/2/issue");
WebResource webResource = client.resource(issueuri);
String issueInput=getIssueInput(); //Here I am creating a json to a string

ClientResponse response = webResource.type("application/json").post(ClientResponse.class, issueInput.getBytes());
System.out.println("\nResponse Status Code = "+response.getStatus());
System.out.println("response.toString() : "+response.toString());

output = response.getEntity(String.class);

So, It is giving a response like

���� �`b�*� �VK��D�]�a<��N�����

Can I get any suggestion on this, exactly why it is creating this problem 

DPKJ
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 1, 2020

I need to try this code out.

0 votes
Matt Doar
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 11, 2020

I think that jira-rest-java-client  was written for Jira Server not Jira Cloud? Not sure of good examples for the cloud

Suggest an answer

Log in or Sign up to answer