Hi everybody,
I'm trying to write a class to create a JIRA issue from a form using the JIRA Rest Java Client. But I am totally lost here, because I can't find any more examples and the examples I found so far brought me to a response code 400 when finally trying to create the issue.
Here's my code:
package de.hlp.jira; import java.net.URI; import java.net.URISyntaxException; import java.util.Collections; import java.util.List; import javax.servlet.http.*; import com.atlassian.jira.rest.client.*; import com.atlassian.jira.rest.client.domain.*; import com.atlassian.jira.rest.client.domain.input.*; import com.atlassian.jira.rest.client.internal.jersey.JerseyJiraRestClientFactory; import com.google.common.collect.Iterables; import com.sun.jersey.api.client.UniformInterfaceException; public class IssueCollector { private static final String FIELD_ID_USER = "#####"; private static final String FIELD_ID_PROBLEM = "customfield_10503"; private static final String FIELD_ID_PARTICIPANTS = "customfield_10501"; private static final String FIELD_ID_PARTICIPANTS_EXT = "customfield_10502"; //public static void createIssue(HttpServletRequest request, String server, String user, String pwd) throws URISyntaxException { public static void createIssue(HttpServletRequest request, String server, String projectId, String user, String pwd) throws URISyntaxException { final JerseyJiraRestClientFactory factory = new JerseyJiraRestClientFactory(); final JiraRestClient restClient = factory.createWithBasicHttpAuthentication(new URI(server), user, pwd); final NullProgressMonitor pm = new NullProgressMonitor(); //final Issue issue = restClient.getIssueClient().getIssue("TST-1", pm); System.out.println("User: "+request.getParameter("user")); IssueRestClient issueClient = restClient.getIssueClient(); final Iterable<CimProject> metadataProjects = issueClient.getCreateIssueMetadata( new GetCreateIssueMetadataOptionsBuilder().withProjectKeys(projectId).withExpandedIssueTypesFields().build(), pm); final CimProject p = metadataProjects.iterator().next(); final CimIssueType issueType = EntityHelper.findEntityById(p.getIssueTypes(), (long) 12); System.out.println("Projects size: "+Iterables.size(metadataProjects)); //Project p = restClient.getProjectClient().getProject(projectId, pm); System.out.println("Project Name: "+p.getName()); /*IssueType issueType = null; for(IssueType it : p.getIssueTypes()) { System.out.println("Project Issue Type: "+it.getId()+", "+it.getName()); if(it.getId() == 12) issueType = it; }*/ if(issueType != null) { //User u = restClient.getUserClient().getUser(request.getParameter("user"), pm); User u = restClient.getUserClient().getUser(user, pm); System.out.println("User found: "+u.getDisplayName()); IssueInputBuilder ib = new IssueInputBuilder(p, issueType, request.getParameter("solution")); System.out.println("Summary set: "+request.getParameter("solution")); System.out.println("IssueInputBuilder created"); ib.setAssignee(u); System.out.println("Assignee set: "+u.getName()); ib.setReporter(u); System.out.println("Reporter set: "+u.getName()); /*String[] participants = request.getParameterValues("participantsName"); ib.setFieldInput(new FieldInput(FIELD_ID_PARTICIPANTS, participants)); System.out.println("Internal participants set"); String[] participantsExt = request.getParameterValues("participantsMail"); ib.setFieldInput(new FieldInput(FIELD_ID_PARTICIPANTS_EXT, participantsExt)); System.out.println("External participants set");*/ ib.setDescription(request.getParameter("solution")); System.out.println("Description set: "+request.getParameter("solution")); List<String> affectedVersionsNames = Collections.emptyList(); ib.setAffectedVersionsNames(affectedVersionsNames); // grab the first component BasicComponent component = null; if(issueType.getField(IssueFieldId.COMPONENTS_FIELD) == null) { System.out.println("COMPONENTS_FIELD=null"); } else { Iterable<Object> allowedValuesForComponents = issueType.getField(IssueFieldId.COMPONENTS_FIELD).getAllowedValues(); if(Iterables.size(allowedValuesForComponents) > 0) { component = (BasicComponent) allowedValuesForComponents.iterator().next(); System.out.println("Component found"); } } // grab the first priority BasicPriority priority = null; if(issueType.getField(IssueFieldId.PRIORITY_FIELD) == null) { System.out.println("PRIORITY_FIELD=null"); } else { Iterable<Object> allowedValuesForPriority = issueType.getField(IssueFieldId.PRIORITY_FIELD).getAllowedValues(); if(Iterables.size(allowedValuesForPriority) > 0) { priority = (BasicPriority) allowedValuesForPriority.iterator().next(); System.out.println("Priority found"); } } if(priority != null && component != null) { ib.setComponents(component).setPriority(priority); } ib.setFieldInput(new FieldInput(FIELD_ID_PROBLEM, request.getParameter("issue"))); System.out.println("Problem description set: "+request.getParameter("issue")); try { final BasicIssue issue = issueClient.createIssue(ib.build(), pm); System.out.println("Issue successfully created."); } catch(Exception e) { if(e.getCause() instanceof UniformInterfaceException) { UniformInterfaceException ex = (UniformInterfaceException) e.getCause(); System.out.println("UniformInterfaceException:"); System.out.println("Status: "+ex.getResponse().getStatus()); System.out.println("Headers: "+ex.getResponse().getHeaders()); System.out.println("Entity: "+ex.getResponse().hasEntity()); } } } } }
(Seems like there's something wrong with the highlighting of the code. Lines 47 to 57 are not commented out!)
The console output is as follows:
User: prgu Projects size: 1 Project Name: Ideenmanagement User found: HLP Summary set: Some solution description IssueInputBuilder created Assignee set: hlp Reporter set: hlp Description set: Some solution description COMPONENTS_FIELD=null PRIORITY_FIELD=null Problem description set: Test Issue UniformInterfaceException: Status: 400 Headers: {Server=[Apache-Coyote/1.1], X-AREQUESTID=[786x25720x1], Set-Cookie=[JSESSIONID=FACF9ACF127548F09CF5CA8768FFF431; Path=/; HttpOnly, crowd.token_key=""; Domain=.org.rational-ag.com; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/; HttpOnly, crowd.token_key=s1Uck8Nyfe71A1wUujOGQg00; Domain=.org.rational-ag.com; Path=/; HttpOnly, atlassian.xsrf.token=BF6X-GYUU-XTRB-DDKD|556b1a6aa78adfabba9ed4818ac05d9dff9769d5|lin; Path=/], X-Seraph-LoginReason=[OUT, OK], X-ASESSIONID=[z4w03a], X-AUSERNAME=[hlp], Cache-Control=[no-cache, no-store, no-transform], Content-Type=[application/json;charset=UTF-8], Transfer-Encoding=[chunked], Date=[Wed, 20 Mar 2013 12:06:21 GMT], Connection=[close]} Entity: false
As far as I can see, there's no response text that would explain why the response status 400 is sent. Right now I have no clue why I can't create the issue. The user has the required rights, creating an issue with that user with the JIRA Web Frontend is possible.
JAR Versions: Tried with jira-rest-java-client-1.0.jar and jira-rest-java-client-1.1-m02.jar, but both lead to the same result.
JIRA Version: v5.2.1#813-sha1:277a546
Can you point me to what might be wrong with my code that causes the 400 error? Thank you so much! :)
Regards,
René
Community moderators have prevented the ability to post new answers.
All I needed was the error messages. I found them using a HTTP sniffer (Wireshark) and searching for the X-AREQUESTID.
Then I was able to see that I was trying to set some of the fields that I didn't have write permission for.
But is there a way to get the error details of a 400 response via the JIRA Rest Java Client?
Does anyone know a way to get the error details of a 400 response with the JIRA Rest Client? Thanks!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I usually get this response too, my understanding is that the request is being sent to JIRA, and then JIRA would return the status 400 error. To catch the really cause I normally do this on the catch block:
try { //creating the issue by sending request to JIRA restClient.getIssueClient().createIssue(ib.build(), pm); } catch (Exception ex1) { Throwable cause = ex1.getCause(); if (cause instanceof UniformInterfaceException) { UniformInterfaceException uie = (UniformInterfaceException) cause; String errMsg = "JIRA's response is[" + uie.getLocalizedMessage() + "], the actual cause is[" + ex1.getLocalizedMessage() + "]"; throw new RuntimeException(errMsg); } throw new RuntimeException(ex1.getLocalizedMessage()); }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
The rest documentation http://docs.atlassian.com/jira/REST/latest/ says:
{ "errorMessages": [ "Field 'priority' is required" ], "errors": {} }
Returned if the input is invalid (e.g. missing required fields, invalid field values, and so forth).
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
My Question was not about the REST API itself, but about the REST Java Client. I'd like to know how to get the error messages using the Java Client.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
have you tried using the com.sun.jersey librarys instead?
I use somthing like:
import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.WebResource;
And craete this method
private static String invokePostMethod(String auth, String url, String data) throws ClientHandlerException { Client client = Client.create(); WebResource webResource = client.resource(url); ClientResponse response = webResource.header("Authorization", "Basic " + auth).type("application/json") .accept("application/json").post(ClientResponse.class, data); int statusCode = response.getStatus(); return response.getEntity(String.class); }
The response should include what you are looking for....
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hey there, Rene.
Perhaps you can consider the assistance via paid services through our Atlassian Experts (http://www.atlassian.com/resources/experts/?tab=find-an-expert).
Not to forget, we also have the Atlassian Community Chat Room for development questions (http://blogs.atlassian.com/developer/2010/01/the_atlassian_community_irc_channel.html). Have a look at the Developers Network page too (https://developer.atlassian.com/display/DOCS/Getting+Involved+in+the+Atlassian+Developer+Network)
Hope this helps.
Warm regards,
Danial
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Community moderators have prevented the ability to post new answers.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.