Присоединение типов файлов к проблеме JIRA с использованием Jersey 2.0 - PullRequest
0 голосов
/ 06 апреля 2019

Я пытаюсь взаимодействовать с JIRA rest API с помощью Jersey 2. В частности, я пытаюсь прикрепить множество типов медиа (текстовые и графические файлы) к проблеме 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()));

Однако я хотел бы поставитьВызов API JIRA в интерфейс с аннотациями, как я сделал это со всеми другими API JIRA ...

@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");

Я хотел бы поместить API-интерфейсы JIRA в интерфейс, так как он более приятен для насмешек.Есть идеи, где я иду не так?Большое спасибо заранее.

...