This site contains the documentation that is relevant to older WSO2 product versions and offerings.
For the latest WSO2 documentation, visit https://wso2.com/documentation/.

Invoking a REST Endpoint from a BPMN Workflow

BPMN REST tasks allow you to invoke REST endpoints within your BPMN processes. This can be achieved by adding a REST Task and handling the REST invocation part in a process implementation. EI-Business Process has provided this functionality out of the box to avoid the hassle of doing implementations by process developers.

Adding a REST task to a process

  1. Create a BPMN process and a BPMN diagram. For instructions, see Creating a BPMN Process.
  2. Drag and drop a REST Task from the tools palette in the place where you need to invoke the endpoint. 
    Drag and drop a REST task
  3. Go to the Properties panel of the REST Task and select the Main config tab. 
  4. Enter values for the following fields appropriately, as shown in the example below.

    Field NameString ValueRequired/Optional
    Service URL 

    URL of the target endpoint


    Optional

    You can give this instead of the Service Reference (EPR).

    BasicAuth UsernameUsername if the endpoints are securedRequired
    BasicAuth PasswordPassword for the username aboveRequired
    Service Reference (EPR)

    Registry resource path of the endpoint reference


    Optional

    You can give this instead of the Service Reference (EPR).

    HTTP MethodHTTP method (GET. POST, PUT or DELETE)Required
    Input PayloadRequest payloadOptional
    Output Variable nameName of the variable to save the responseOptional
    Output Variable Mappings

    Variable mappings to map JSON response content into multiple variables . 

    var2:customer.name,var3:item.price
    Optional
    Transport headers

    Header values in the format

    {"headerName1":"headerValue1","headerName2":"header Value2"}
    Optional

The following configuration provides an example definition to use as the REST task.

<serviceTask id="InvokeRESTEndpoint" name="REST task1" activiti:class="org.wso2.carbon.bpmn.extensions.rest.RESTTask">
    <extensionElements>
        <activiti:field name="serviceURL">
            <activiti:expression>http://10.0.3.1:9773/restSample1_1.0.0/services/rest_sample1/${method}
            </activiti:expression>
        </activiti:field>
        <activiti:field name="basicAuthUsername">
            <activiti:expression>bobcat</activiti:expression>
        </activiti:field>
        <activiti:field name="basicAuthPassword">
            <activiti:expression>bobcat</activiti:expression>
        </activiti:field>
        <activiti:field name="method">
            <activiti:string>
                <![CDATA[POST]]>
            </activiti:string>
        </activiti:field>
        <activiti:field name="input">
            <activiti:expression>Input for task1</activiti:expression>
        </activiti:field>
        <activiti:field name="outputVariable">
            <activiti:string>
                <![CDATA[v1]]>
            </activiti:string>
        </activiti:field>
        <activiti:field name="headers">
            <activiti:string>
                <![CDATA[{"headerName1":"headerValue1","headerName2":"header Value2"}]]>
            </activiti:string>
        </activiti:field>
    </extensionElements>
</serviceTask>

Using JSON payloads

The following configuration provides an example with JSON input sent with the request. You can use the Output Variable Mappings field to map the JSON response to the variables. 

<serviceTask id="servicetask2" name="Rest task2" activiti:class="org.wso2.carbon.bpmn.extensions.rest.RESTTask">
    <extensionElements>
        <activiti:field name="serviceRef">
            <activiti:expression>conf:/test1/service2</activiti:expression>
        </activiti:field>
        <activiti:field name="method">
            <activiti:string>
                <![CDATA[POST]]>
            </activiti:string>
        </activiti:field>
        <activiti:field name="input">
            <activiti:expression>{ "companyName":"ibm", "industry":"${industry}", "address":{ "country":"USA", "state":"${state}"} }
            </activiti:expression>
        </activiti:field>
        <activiti:field name="outputMappings">
            <activiti:string>
                <![CDATA[var2:customer.name,var3:item.price]]>
            </activiti:string>
        </activiti:field>
    </extensionElements>
</serviceTask>

Changing the endpoint after process deployment

The REST endpoint can not be changed after deploying the process using the above method. If you want to change the endpoint after deploying the process, you need to point to a registry location, which contains an endpoint reference. For more information on how to do this, see Endpoint References.

To change the REST endpoint after process deployment, give the registry location of the service reference in the Service Reference (EPR) field on the  Main config  tab of the Properties panel for the REST task. For an example, see the above example Using JSON payloads. 

Configuring the HTTP client

The above REST task is bundled with an HTTP client by default. You can edit its configurations in the <EI_HOME>/wso2/business-process/conf/activiti.xml file as shown in the example below.

<bean id="restClientConfiguration">
        <property name="maxTotalConnections" value="200"/>
        <property name="maxConnectionsPerRoute" value="200"/>
        <property name="connectionTimeout" value="12000"/>
        <property name="socketTimeout" value="12000"/>
</bean>