Getting data from a simple REST service
In this tutorial, we try to call a public REST service from a task in Bonita. We will modify the tutorial Getting a target resource based on the client request to create a simple service that return the user's information in XML format. Then, we'll create a business process using Bonita with a simple task. On this task, we create a connector that calls this service and parser the user's information.
Create the REST service
-
Open the class UserResource.java file. Modify the code as following:
package forthREST;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;
public class UserResource extends ServerResource {
@Get
public String toString() {
String uid = (String) getRequestAttributes().get("uid");
if (uid.equals("123")){
return "<users>" +
"<user>" +
"<id>"+uid+"</id>" +
"<name>John</name>" +
"</user>" +
"</users>";
}
return "No information found.";
}
}
We suppose that the REST service will return the user's information in XML format. To simplify the example, we return a string in XML format for only the user whose ID is 123 as given in the source codes above.
Now, run the modified REST service by right clicking on the RESTDistributor.java file, select Run As → Java Application.
-
Create business process to access this REST resource
On this part, we'll create a simple business process that accesses to the user resource provided by the REST service above. We'll use the Groovy script connector to retrieved the name of user 123.
Open Bonita Studio. Click on New button to create a new diagram.
Design a process as following, where
Step2 is a service task and
Step3 is a human and is just for previewing the result returned from the REST service.
create the global variable name of type text.
Click on
Step2. In the
Execution→Connectors in tab, click on
Access the Bonita Marketplace.
Select the REST connector and click Next.
Select
REST from the left panel and
GET from the right panel, then
Next.
Name the connector as RestService_Connector, then Next.
-
Keep clicking
Next until reaching the
Output Operations step shown below.
Choose the variable
name from the first list to store the connector output.
Select the edit pencil icon in the second list.
Add the following code in the REST editor:
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
//the response is in XML, so we parse the XML bodyAsString
DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
InputSource inputSource = new InputSource();
inputSource.setCharacterStream(new StringReader(bodyAsString));
Document userDataDocument = documentBuilder.parse(inputSource);
//get the root element users
Node usersNode = userDataDocument.getDocumentElement();
//get the childs nodes of the node users (the list contains one child which is user)
NodeList usersChildNodes = usersNode.getChildNodes();
//get the first child (which is user)
Node userNode = usersChildNodes.item(0);
//get the childs of user
NodeList userChildNodes = userNode.getChildNodes();
String userName="";
//search for the child node "name"
for (int i=0; i<userChildNodes.getLength(); i++)
{
Node node = userChildNodes.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE)
{
String key = node.getNodeName();
if(key.equals("name"))
{
String value = node.getTextContent();
userName=value;
break;
}
}
}
return userName;
Click
Ok.
Click Finish.
Save the process.
Let's design a simple form for Step3.
Click on
Step3. Click on
Execution→Form. In
Target Form,
create new Form called
outputForm.
In the Form page, drag and drop a Text field, then name it Result.
The
Result text field will display the result of the web service which will be stored in the variable
name. To do so, click on create a new variable in the form. Enter the details as shown below.
Click on
Save, then click on the
Result text field. In the right side panel, go to
Value and fill it with
result.value.
Save and close the form.
Make sure that the web service is still listening on port 8182.
Click on the Configure tag. Map the Employee actor to your organization Staff group and Employee role.
Save changes, then Run the process.
Start the pool process.
The result of the web service will be displayed in the
Result text field as shown below.
Exercises
Modify the business process above to allow inputting the user's ID to request instead of embedding it in the
URI. For example, instead of requesting the resource at
http://localhost:8182/users/123/, the process allows requesting flexibly resources at
http://localhost:8182/users/[userID]/ where the [userID] is provided by the initiator of the business process.
Add more user items to the REST service. Then, modify the business process to receive list of existing users' names.
Modify the REST service to return the user's information in JSON format. Then, update the business process's connector to achieve the provided user's names.
Back to top