我在向nifi发送简单的PUT请求时遇到麻烦,基本上我想用Java代码启动nifi处理器。
错误消息-服务器返回HTTP响应代码:405的URL:http://localhost:8080/nifi-api/processors/
我应该采取什么步骤解决此405问题? nifi api网站提供了有关rest api的高级文档,但是没有有关如何使用Java向NiFi发出正确的PUT请求的指南。
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
public class HttpURLConnectionExample {
private final String USER_AGENT = "Mozilla/5.0";
public static void main(String[] args) throws Exception {
HttpURLConnectionExample http = new HttpURLConnectionExample();
System.out.println("\nTesting 2 - Send Http POST request");
http.sendPut();
}
// HTTP PUT request
private void sendPut() throws Exception {
String url = "http://localhost:8080/nifi-api/processors/";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
//add reuqest header
con.setRequestMethod("PUT");
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Accept", "application/json");
con.setRequestProperty("text/javascript", "*/*");
String urlParameters = "{\r\n" +
" \"status\": {\r\n" +
" \"runStatus\": \"RUNNING\"\r\n" +
" },\r\n" +
" \"component\": {\r\n" +
" \"state\": \"RUNNING\",\r\n" +
" \"id\": \"749b3133-0162-1000-9acc-f384457b160c\"\r\n" +
" },\r\n" +
" \"id\": \"749b3133-0162-1000-9acc-f384457b160c\",\r\n" +
" \"revisions\": {\r\n" +
" \"version\": 1,\r\n" +
" \"clientId\": \"550c4b84-0165-1000-37be-724ed17b5329\"\r\n" +
"}\r\n" +
" ";
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
}
}
答案 0 :(得分:4)
A 405表示所使用的URL不支持HTTP方法(在这种情况下为PUT)。这是因为/ processors仅支持GET请求以检索可用的处理器。为了更新处理器,URL为/ processors / {processorId},其中用要更新的给定处理器ID替换了ProcessorId。
通常,学习NiFi的REST API的最好方法是打开一个工具,例如Chrome的Dev工具,然后查看网络以查看在从UI执行所需操作时发出的请求。这些将显示URL,参数和请求/响应正文。
更新
这是UI在启动UpdateAttribute处理器时发送的内容,该处理器将PUT设置为http://localhost:8080/nifi-api/processors/5817008a-0165-1000-0bf3-2c44fde14c37
{
"revision":{
"clientId":"61a9ab71-0165-1000-777c-c7538789246c",
"version":0
},
"component":{
"id":"5817008a-0165-1000-0bf3-2c44fde14c37",
"state":"RUNNING"
},
"disconnectedNodeAcknowledged":false
}