使用Java中的Rest API在apache NiFi中上传模板

时间:2017-09-28 11:44:55

标签: java apache rest apache-nifi

我正在尝试创建一个REST API调用,将模板导入到我的NiFi UI帖子中,该帖子实例化。

以下是我尝试过的代码,

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());

以下是我尝试从

导入的BalaBackUp.xml文件中的{id}
<?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>

我的响应代码为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]}}
你可以帮我解决一下我所缺少的事情吗?

2 个答案:

答案 0 :(得分:1)

应使用DefaultHttpClient()而不是HttpClientBuilder构建HttpClient。以下是代码段。此外,您必须addPart名称&#39;模板&#39;让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?

答案 1 :(得分:0)

您需要检查logs/nifi-app.log文件中的错误,该错误将解释HTTP 500异常是什么。当您通过Apache NiFi UI上传模板时,请尝试使用浏览器的开发人员工具面板检查基础网络请求,因为这些将是以编程方式执行此操作所需的相同请求。

我假设复制的模板XML不完整,因为它不是有效的模板,如果按原样提供,肯定会导致内部服务器异常。