我可以使用其余客户端 API 创建 jira 问题,但我无法将屏幕截图/附件附加到现有的
JIRA
问题。如果有人能提供一个解决方案,那将非常有帮助,我们将不胜感激。
我刚刚编写了以下代码片段,用于将 jpeg 文件附加到现有的
JIRA Issue
。但后来我经历了“线程“main”中的异常java.lang.NoClassDefFoundError: org/apache/commons/httpclient/HttpConnectionManager"
代码片段:-
私有静态字符串addAttachment(字符串附件文件路径)抛出URISyntaxException,FileNotFoundException{
final java.net.URI jiraServerUri = new java.net.URI("https://oliveits.atlassian.net");
FileInputStream fileStreamPath = new FileInputStream(attachmentfilenamepath);
JerseyJiraRestClientFactory factory = new JerseyJiraRestClientFactory();
NullProgressMonitor pm = new NullProgressMonitor();
System.out.println("Server Url :"+jiraServerUri);
JiraRestClient restClient = factory.createWithBasicHttpAuthentication(jiraServerUri,"mobileqa","MobileQA1234");
Issue issue1 = restClient.getIssueClient().getIssue(newKey, pm);
final java.net.URI AttachmentUri = new java.net.URI(jiraServerUri+"/rest/api/2/issue/"+newKey+"/attachments");
System.out.println("URI :"+issue1.getAttachmentsUri());
//restClient.getIssueClient().addAttachment(pm,issue1.getAttachmentsUri(), fileStreamPath , imageName);
restClient.getIssueClient().addAttachment(pm, AttachmentUri, fileStreamPath, imageName);
return attachmentfilepath;
}
例外:-
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/httpclient/HttpConnectionManager
at com.atlassian.jira.rest.client.internal.jersey.JerseyJiraRestClientFactory.create(JerseyJiraRestClientFactory.java:34)
at com.atlassian.jira.rest.client.internal.jersey.JerseyJiraRestClientFactory.createWithBasicHttpAuthentication(JerseyJiraRestClientFactory.java:39)
at com.jtricks.JTricksRESTClient.addAttachment(JTricksRESTClient.java:157)
at com.jtricks.JTricksRESTClient.main(JTricksRESTClient.java:101)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.httpclient.HttpConnectionManager
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 4 more
刚刚将缺少的 jar 文件添加到类路径中。但我遇到了一个例外,如下所述。
Exception Msg-1:com.sun.jersey.api.client.ClientHandlerException:Java 类 com.sun.jersey.multipart.MultiPart 和 Java 类型类 com.sun.jersey.multipart.MultiPart 的消息正文编写器,以及MIME 媒体类型 multipart/form-data;边界=Boundary_1_18541827_1358346116249 未找到
Exception Msg-2:com.sun.jersey.api.client.ClientHandlerException:Java 类 com.sun.jersey.multipart.MultiPart 和 Java 类型类 com.sun.jersey.multipart.MultiPart 的消息正文编写器,以及MIME 媒体类型 multipart/form-data;边界=Boundary_1_18541827_1358346116249 未找到
您的问题来自于无法找到
org.apache.commons.httpclient.HttpConnectionManager
课程。在类路径中查找 commons-httpclient
JAR。
你可以试试这个:
String issueKey = "your-issue";
JiraRestClient restClient
= AsynchronousJiraRestClientFactory()
.createWithBasicHttpAuthentication(
getJiraUri(),
username,
password);
Issue issue = restClient.getIssueClient().getIssue(issueKey).claim();
restClient.getIssueClient().addAttachments(issue.getAttachmentsUri(), file).claim();
restClient.close();
在 pom.xml 中
<properties>
<jira-rest-java-client-core.version>5.2.4</jira-rest-java-client-core.version>
<atlassian.fugue.version>5.0.0</atlassian.fugue.version>
</properties>
...
<dependencies>
...
<dependency>
<groupId>com.atlassian.jira</groupId>
<artifactId>jira-rest-java-client-core</artifactId>
<version>${jira-rest-java-client-core.version}</version>
</dependency>
<dependency>
<groupId>com.atlassian.jira</groupId>
<artifactId>jira-rest-java-client-api</artifactId>
<version>${jira-rest-java-client-core.version}</version>
</dependency>
<dependency>
<groupId>io.atlassian.fugue</groupId>
<artifactId>fugue</artifactId>
<version>${atlassian.fugue.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
如果您想将附件添加到特定类别
您可以添加附件(无特定类别):
String auth = new String(org.apache.commons.codec.binary.Base64
.encodeBase64((jiraUsername + ":" + jiraPassword).getBytes()));
URI myJiraUri = URI.create(jiraUrl);
URI myJiraUri = URI.create(jiraUrl);
restClient = new AsynchronousJiraRestClientFactory().createWithBasicHttpAuthentication(myJiraUri,
jiraUsername, jiraPassword);
private String attachFileToIssue(JiraRestClient restClient, String jiraIssueId, String auth, String fileName,
File file) throws IOException {
Issue issue = restClient.getIssueClient().getIssue(jiraIssueId).claim();
HttpPost toAttchFilePost = new HttpPost(issue.getAttachmentsUri());
toAttchFilePost.setHeader("X-Atlassian-Token", "nocheck");
toAttchFilePost.setHeader("Authorization", "Basic " + auth);
HttpEntity requestEntity = MultipartEntityBuilder.create().addPart("file", new FileBody(file)).build();
toAttchFilePost.setEntity(requestEntity);
LOG.info("send file to attachement" + toAttchFilePost.toString());
CloseableHttpClient client = HttpClients.createDefault();
CloseableHttpResponse responseToAttchFile = (CloseableHttpResponse) client.execute(toAttchFilePost);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
responseToAttchFile.getEntity().writeTo(baos);
LOG.info("attachement in uncategorized status : " + responseToAttchFile.getStatusLine().getStatusCode());
if (responseToAttchFile.getStatusLine().getStatusCode() == 200) {
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(baos.toString());
String attachments = jsonNode.get(0).get("id").asText();
return attachments;
} else {
System.err.println("Failed to attach file. Status: " + responseToAttchFile.getStatusLine());
return null;
}
}
然后将文件移动到特定类别:
HttpPost post = new HttpPost(jiraUrl + "rest/attach-cat/1.0/attachments/move");
post.setHeader("Authorization", "Basic " + auth);
post.setHeader("Content-Type", "application/x-www-form-urlencoded");
String formData = "toCatId=" + toCatId + "&attachments=" + attachmentId + "&issueKey=" + jiraIssueId;
post.setEntity(new StringEntity(formData));
try (CloseableHttpClient client = HttpClients.createDefault();
CloseableHttpResponse response = client.execute(post)) {
if (response.getStatusLine().getStatusCode() == 200) {
return true;
} else {
System.err.println("Failed to move file. Status: " + response.getStatusLine());
return false;
}
}
注意: