0

我目前在一个项目中使用 RT 4.4.3,我正在尝试使用 Java 代码创建一个带有附件的新票。

我尝试按照托管在 GitHub 上的BestPractical 资源提供的说明进行操作,并在此拉取列表中指定。

尝试执行该操作的代码片段如下:

PostMethod mPost = new PostMethod(TicketListConstants.SEGNALAZIONI_RTIR_URI + "/ticket");

        mPost.setRequestHeader("Content-type", "application/json");
        mPost.setRequestHeader("Authorization", TicketListConstants.SEGNALAZIONI_RTIR_TOKEN);

        /*String json = ;
        NameValuePair[] data = {
                new NameValuePair("content", json)
        };*/
        UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(request);
        File file = uploadRequest.getFile("fileName");
        String filename = uploadRequest.getFileName("fileName");

        byte[] filecontent = this.encodeBase64(file);

        mPost.setRequestBody("{ \"Queue\": \"Infosharing\", \"Subject\": \""+subject+"\",\"From\":\""+currentUser.getEmailAddress()+"\",\"To\":\"test@liferay.com\",\"Owner\":\""
                                +currentUser.getEmailAddress()+"\",\"Requestor\":\""+currentUser.getEmailAddress()+"\",\"Content\":\""+description+"\",\"AttachmentsContents\":[{\"FileName\":\""+filename+"\",\"FileType\":\"application/pdf\",\"FileContent\":\""+filecontent+"\"}]}");
        HttpClient cl = new HttpClient();
        String result = "";
        String newId = "";
        try {
            cl.executeMethod(mPost);
            result = mPost.getResponseBodyAsString();

            if (result != null) {
                JSONObject json = null;
                try {
                    json = JSONFactoryUtil.createJSONObject(result);
                } catch (JSONException e) {
                    _log.error("Error extracting ticket info: "+e.getMessage());
                }
                newId = json.getString("id");
            }
        } catch (UnsupportedEncodingException e){
            _log.error("Error in searching tickets: "+e.getMessage());
        } catch (IOException io) {
            _log.error("Error in searching tickets: "+io.getMessage());
        }

所以我发送到 RT 的 JSON 如下:

{ "Queue": "Infosharing", "Subject": "Tutto in uno","From":"test@liferay.com","To":"test@liferay.com","Owner":"test@liferay.com","Requestor":"test@liferay.com","Content":"Aggiungo tutto in un solo passaggio","AttachmentsContents":[{"FileName":"prova.txt","FileType":"plain/text","FileContent":""}]}

问题是票证已正确创建,但未添加附件。

我也尝试使用 SOAPUI 执行相同的操作,但即使响应没有任何错误,也不会将附件添加到票证中。

有人可以帮我做错什么吗?

编辑 2019-06-10:因为据报道至少到 2018 年 12 月底:

创建附件 目前 RT 不允许通过其 API 创建附件。

https://rt-wiki.bestpractical.com/wiki/REST#Ticket_Attachment

但是作为临时解决方法,应该可以将附件发布到票的评论中,任何人都可以帮助找到解决此问题的方法吗?

4

1 回答 1

0

由于我无法测试你的代码,我建议你使用 HttpClient 4,我在下面提供了一个示例代码片段。根据您的要求修改代码并尝试检查。

HttpPost post = new HttpPost("http://rtserver.com");
FileBody fileBody = new FileBody(file, ContentType.DEFAULT_BINARY);
StringBody stringBody1 = new StringBody("Message 1", ContentType.MULTIPART_FORM_DATA);

MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addPart("upfile", fileBody);
builder.addPart("text1", stringBody1);

HttpEntity entity = builder.build();

post.setEntity(entity);

HttpResponse response = client.execute(post);
于 2019-06-07T15:15:29.420 回答