2017-09-28 55 views
0

我正在嘗試創建一個REST API調用,以將模板導入到我的NiFi UI後,其實例化相同。在Java中使用Rest API上傳apache NiFi中的模板

下面是從我試圖從

<?xml version="1.0" ?> 
<template encoding-version="1.1"> 
    <description></description> 
    <groupId>bd5dba8b-015d-1000-1fd5-450ede38b7a5</groupId> 
    <name>BalaBackUp</name> 
    <snippet> 
    <processGroups> 
     <id>f80896d4-c71f-3395-0000-000000000000</id> 
     <parentGroupId>29a5776d-9728-3fee-0000-000000000000</parentGroupId> 
     <position> 
     <x>0.0</x> 
     <y>0.0</y> 
     </position> 
     <comments></comments> 
     <contents> 
     <connections> 
      <id>c0d0e26d-5ee2-3d60-0000-000000000000</id> 
      <parentGroupId>f80896d4-c71f-3395-0000-000000000000</parentGroupId> 
      <backPressureDataSizeThreshold>1 GB</backPressureDataSizeThreshold> 
      <backPressureObjectThreshold>10000</backPressureObjectThreshold> 
      <destination> 
      <groupId>f80896d4-c71f-3395-0000-000000000000</groupId> 
      <id>1f9e926a-71fc-356f-0000-000000000000</id> 
      <type>PROCESSOR</type> 

我得到的響應導入BalaBackUp.xml文件,我嘗試了代碼,

String siteUrl = "localhost"; 
String portNumber = "8080"; 
String pId = "f80896d4-c71f-3395-d527-8c6bd69f44d0"; 
String pathname = "D:\\Users\\bramasam\\Downloads\\BalaBackUp.xml"; 
String restString = "http://" + siteUrl + ":" + portNumber + "/nifi-api/process-groups/" + pId + "/templates/upload"; 
HttpPost httpPost = new HttpPost(restString); 

File fileObj = new File(pathname); 

httpPost.addHeader("Content-type", "multipart/form-data"); 

FileEntity fileEntity = new FileEntity(fileObj, ContentType.MULTIPART_FORM_DATA); 

httpPost.setEntity(fileEntity); 

HttpClient httpClient = HttpClientBuilder.create().build(); 
HttpResponse response = httpClient.execute(httpPost); 
StatusLine status = response.getStatusLine(); 
System.out.println(status.getStatusCode()); 

下面是{ID}代碼爲500,並帶有以下回應

HttpResponseProxy{HTTP/1.1 500 Internal Server Error [Date: Thu, 28 Sep 2017 09:43:28 GMT, X-Frame-Options: SAMEORIGIN, Content-Type: text/plain, Transfer-Encoding: chunked, Server: Jetty(9.4.3.v20170317)] ResponseEntityProxy{[Content-Type: text/plain,Chunked: true]}} 

你能幫我解答我失蹤的事嗎?

回答

1

應該使用DefaultHttpClient()而不是HttpClientBuilder來構建HttpClient。以下是代碼片段。此外,你必須addPart名稱爲「模板」的nifi將其識別爲模板

File file = new File(pathname); 

HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httpPost = new HttpPost(restString); 

FileBody uploadFilePart = new FileBody(file); 
MultipartEntity reqEntity = new MultipartEntity(); 
reqEntity.addPart("template", uploadFilePart); 
httpPost.setEntity(reqEntity); 

HttpResponse response = httpclient.execute(httpPost); 

參考網址下面的MultipartEntity() POST要求 How can I make a multipart/form-data POST request using Java?

0

您需要檢查logs/nifi-app.log文件中的錯誤,該錯誤將解釋HTTP 500例外情況。嘗試使用瀏覽器的開發人員工具面板在通過Apache NiFi UI上傳模板時檢查底層網絡請求,因爲這些將以編程方式執行此操作所需的相同請求。

我假設複製的模板XML不完整,因爲這不是一個有效的模板,並且如果按原樣提供,肯定會導致內部服務器異常。

+0

我驗證XML文件,它是完整的(而且我能夠手動上傳)。另外,我檢查了nifi-app.log,但是找不到HTTP 500 Exception的任何錯誤。是我將XML文件構建爲「multipart/form-data」權利的方式。 –

+0

也許是這樣,但是你的XML文件只顯示24行,並且它不關閉任何開始標記。對我來說可見的是無效的XML。內部NiFi框架或處理器執行中的任何錯誤都會導致將錯誤堆棧跟蹤打印到nifi-app日誌中。我不知道爲什麼你無法看到導致HTTP 500錯誤的錯誤。 – Andy

+0

對不起,溝通錯誤。我只粘貼了XML示例以顯示我從哪裏獲得{id}字段。它是一個完整的XML。 –