我正在尝试使用Jersey 2与JIRA rest API进行交互。具体来说,我正在尝试将多种媒体类型(文本和图像文件)附加到JIRA问题上。下面是一些示例代码
// given
IssueService issueService = new IssueService();
final String issueIdOrKey = "Test-25", fields = "*all", expand = "", properties = "*all";
Issue issue = issueService.getIssue(issueIdOrKey, fields, expand, properties);
// when
String fileName = new SimpleDateFormat("yyyyMMddHHmm").format(new Date());
File issueTextAttachment = new File(TEST_OUTPUT_DIR + fileName + ".txt");
FileUtils.writeStringToFile(issueTextAttachment, "Text Attachment", "UTF-8");
BufferedImage image = new BufferedImage(500, 500, BufferedImage.TYPE_BYTE_GRAY);
File issueImageAttachment = new File(TEST_OUTPUT_DIR + fileName + ".jpg");
ImageIO.write(image, "jpg", new FileOutputStream(issueImageAttachment));
issue.addAttachment(issueTextAttachment);
issue.addAttachment(issueImageAttachment);
String idOrKey = issue.getId() == null ? issue.getKey() : issue.getId();
FormDataMultiPart form = new FormDataMultiPart();
for (int i = 0; i < files.size(); i++) {
// The name of the multipart/form-data parameter that contains attachments must be "file"
FileDataBodyPart fdp = new FileDataBodyPart("file", files.get(i));
form.bodyPart(fdp);
}
下一部分代码逻辑的工作原理是,它成功地附加到JIRA问题上...
Response response = client.getClient().target(JIRA_INSTANCE+ "/rest/api/2/issue/" + idOrKey + "/attachments")
.request(MediaType.APPLICATION_JSON).header("X-Atlassian-Token","nocheck")
.post(Entity.entity(form, form.getMediaType()));
但是我想将JIRA API调用放入带有注释的接口中,就像我对所有其他JIRA API所做的那样...
@Consumes({"application/json"})
@Produces({"application/json"})
@Path("/rest/api/2/")
public interface JiraAPI {
@GET
@Path("/search")
@Produces(MediaType.APPLICATION_JSON)
Response search(@QueryParam("jql") final String jql,
@QueryParam("startAt") final int startAt,
@QueryParam("maxResults") final int maxResults,
@QueryParam("validateQuery") final Boolean validateQuery,
@QueryParam("fields") final String fields,
@QueryParam("expand") final String expand
);
@POST
@Path("/issue/{projectIdOrKey}/attachments")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
Response addAttachment(@PathParam("projectIdOrKey") final String issueIdOrKey,
@FormDataParam("file") final FormDataMultiPart file,
@DefaultValue("nocheck") @HeaderParam("X-Atlassian-Token") final String xAtlassianToken);
}
我怀疑我定义的最后一个API错误。就调用接口中定义的API而言,逻辑是这样的。
WebTarget webTarget = client.getClient().target(JIRA_INSTANCE);
JiraAPI jiraAPI = WebResourceFactory.newResource(JiraAPI.class, webTarget);
Response response = jiraAPI.addAttachment(idOrKey, form, "nocheck");
我想将JIRA API放到接口中,因为它更适合模拟。关于我要去哪儿有什么想法吗?预先非常感谢。