How does camunda call external Web service/Http service

1. In Camunda, Java or JavaScript can be used to call external Web services. The following is an example of implementing a web service call using Java:

URL url = new URL(“http://example.com/api”);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod(“POST”);
connection.setRequestProperty(“Content-Type”, “application/json”);
connection.setDoOutput(true);

OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write(“{‘param1’: ‘value1’, ‘param2’: ‘value2’}”);
out.close();

InputStream inputStream = connection.getInputStream();


In this example, Java's HttpURLConnection class is used to invoke the Web service. First, create a URL object to specify the address of the Web service. Then, open a HttpURLConnection connection, and set the request method and request header. Next, write the request body to the connection's output stream, then close the stream. Finally, read the web service's response from the connection's input stream. 2. In the Service Task node of Camunda, the above code can be used to call the Web service. For example, the following code could be added to the execute() method of a Java class:

URL url = new URL(“http://example.com/api”);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod(“POST”);
connection.setRequestProperty(“Content-Type”, “application/json”);
connection.setDoOutput(true);

OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write(“{‘param1’: ‘value1’, ‘param2’: ‘value2’}”);
out.close();

InputStream inputStream = connection.getInputStream();

String responseBody = new String(inputStream.readAllBytes());
execution.setVariable(“responseBody”, responseBody);

In this example, first create a URL object, then use the HttpURLConnection class to open the connection and set the request method and headers. Then, write the request body to the connection's output stream, and close the stream. Next, read the web service's response from the connection's input stream and store the response body as a string in a process variable. Finally, the expression language can be used in the BPMN model to access process variables to obtain responses from Web services.

Guess you like

Origin blog.csdn.net/wxz258/article/details/130721654