Creating a REST Web Service
Create a Job that provides a RESTful Web Service endpoint to create a Formbird Document.
Items used
| Type | Name |
|---|---|
| Routines | FormbirdRoutine |
| Components | tRESTRequest, tRESTResponse, tJava, tJavaRow, tRunJob |
Overview
- Setup a Main Job (Job 1) as a REST endpoint that calls a Sub Job (Job 2)
- Setup a Sub Job (Job 2) that creates a Formbird Document using provided input
1. REST endpoint (Main Job) ==> 2. Formbird Insert (Sub Job)
Setup Main Job
Create a new Job, and name it "CreateAssetService". This Job sets up a REST endpoint to trigger the Sub Job.
-
Setup your Job's
Context Variables -
Select the Jobs Context tab, and create the Context Variables as defined in
Contexts and Configuration Filessection of this documentation. Specify your actual Formbird URLs and credentials during setup in order to run the Job within Talend Studio. -
Add a
tRESTRequestcomponent -
Configure the REST Endpoint to "/rest/FormbirdService"
- Configure a REST API Mapping as follows:
- HTTP Verb: POST
- URL Pattern: "/createAsset"
- Consumes: JSON
- Produces: JSON
-
Select the ellipses button on the new ReST API Mapping and add a record as follows:
- Column:
body - Type: String
- Column:
-
Add an Object context variable named
requestDocument. -
Add an Object context variable named
responseDocument. -
Add a
tJavaRowcomponent to theJob, and copy the following code to it. -
Under the component's
Advanced settingspanel, add:```java import org.apache.log4j.Logger;
import com.fieldtec.webclient.config.WebclientConfig; import com.fieldtec.webclient.model.Document; import com.fieldtec.webclient.service.FieldtecService; import com.fieldtec.webclient.service.impl.FieldtecServiceImpl; import com.fieldtec.webclient.util.UtilJson; ```
-
Under the component's
Basic settingspanel, add:```java Logger logger = Logger.getLogger(context.loggerName); String method = jobName + "." + currentComponent;
// clear context context.queueItem = null; context.requestDocument = null; context.responseDocument = null;
logger.info(method + " Received Request"); logger.debug(method + " Received Request body is " + input_row.body);
// set context.requestDocument context.requestDocument = input_row.body;
// set context.responseBean com.fieldtec.webclient.model.Document responseDocument = new com.fieldtec.webclient.model.Document(); context.responseDocument = responseDocument;
logger.info(method + " call subJob"); ```
-
Add a
tRunJobcomponent and select a sub Job to run. SelectTransmit whole contextto pass context values to the sub Job. DeselectDie on child error. -
Add a
tJavacomponent, and copy the following code to it. -
Under the component's
Advanced settingspanel, add:java import org.apache.log4j.Logger; -
Under the component's
Basic settingspanel, add:java Logger logger = Logger.getLogger(context.loggerName); String method = jobName + "." + currentComponent; logger.info(method + " onSubjobOk"); -
Add a
tJavaRowcomponent, and copy the following code to it. -
Under the component's
Advanced settingspanel, add:```java import org.apache.log4j.Logger;
import com.fieldtec.webclient.model.Document; import com.fieldtec.webclient.util.UtilJson; ```
-
Under the component's
Basic settingspanel, add:```java method = jobName + "." + currentComponent; logger.info(method + " process responseDocument");
com.fieldtec.webclient.model.Document responseDocument = (com.fieldtec.webclient.model.Document) context.responseDocument; String result = UtilJson.prettyPrint(responseDocument); logger.info(method + " result is " + result);
output_row.body = result; logger.info(method + " return result in response body"); ```
-
Add a
tRESTResponsecomponent and configure as follows: -
Return Body Type: String
- Return status code: OK (200)
-
Pass
bodyas the schema -
Connect the
tRESTRequestcomponent [step 2] to thetJavaRowcomponent [step 5] using aMainRow link. Passbodyas the schema. -
Connect the
tJavaRowcomponent [step 5] to thetRunJobcomponent [step 6] using anRun Iftrigger, and specify the trigger as: "context.requestDocument != null". -
Connect the
tRunJobcomponent [step 6] to thetJavacomponent [step 7] using anOn Subjob Oktrigger. -
Connect the
tJavacomponent [step 7] to thetJavaRowcomponent [step 8] using aMainRow link. Passbodyas the schema. -
Connect the
tJavaRowcomponent [step 8] to thetRESTResponsecomponent [step 9] using aMainRow link. Passbodyas the schema.
Setup Sub Job
Create a new Job, and name it "CreateAssetService_action". This Job creates a Formbird document.
-
Setup your Job's
Context Variables -
Select the Jobs Context tab, and create the Context Variables as defined in
Contexts and Configuration Filessection of this documentation. Specify your actual Formbird URLs and credentials during setup in order to run the Job within Talend Studio. -
Add an Object context variable named
requestDocument. -
Add an Object context variable named
responseDocument. -
Add a
tJavacomponent to theJob, and copy the following code to it. -
Under the component's
Advanced settingspanel, add:```java import org.apache.log4j.Logger;
import com.fieldtec.webclient.config.WebclientConfig; import com.fieldtec.webclient.model.Document; import com.fieldtec.webclient.service.FieldtecService; import com.fieldtec.webclient.service.impl.FieldtecServiceImpl; import com.fieldtec.webclient.util.UtilJson; ```
-
Under the component's
Basic settingspanel, add:```java Logger logger = Logger.getLogger(context.loggerName); String method = jobName + "." + currentComponent;
FieldtecService ftService = null;
com.fieldtec.webclient.model.Document responseDoc = null;
try { // Initialise ftService (always synchronize context and then setup webclientConfig) context.synchronizeContext(); FormbirdRoutine.setupWebClientConfig(context); ftService = new FieldtecServiceImpl();
String assetTemplateId = "5ba9bbee5b116026f1b23dad"; com.fieldtec.webclient.model.Document requestDocument = (com.fieldtec.webclient.model.Document) context.requestDocument; com.fieldtec.webclient.model.Document doc = new Document(); doc.setValue("assetId", requestDocument.getValue("assetId")); doc.setValue("name", requestDocument.getValue("assetName"); doc.setValue("description", requestDocument.getValue("assetDesc"); doc.setValue(FormbirdRoutine.KEY_SYSTEMHEADER_TEMPLATE_ID, assetTemplateId); // Insert document Document responseDocument = ftService.insert(doc); if (responseDocument != null) { // Get documentId to log String documentId = (String) responseDocument.getValue(FormbirdRoutine.KEY_DOCUMENT_ID); logger.info("Inserted: " + documentId); logger.info(UtilJson.prettyPrint(responseDocument)); responseDoc = new com.fieldtec.webclient.model.Document(); responseDoc.setValue("status", "Success"); responseDoc.setValue("message", documentId); } else { // Handle error logger.error("Error inserting document"); responseDoc = new com.fieldtec.webclient.model.Document(); responseDoc.setValue("status", "Error"); responseDoc.setValue("message", "Error encountered inserting document"); }} finally { if (responseDoc == null) { responseDoc = new com.fieldtec.webclient.model.Document(); responseDoc.setValue("status", "Error"); responseDoc.setValue("message", "Error encountered inserting document"); }
// Set context variable with response for REST caller context.responseDocument = responseDoc; if (ftService != null) { ftService.close(); }} ```
Run the Main Job
- Return to the Main Job.
- Navigate to the Job's Run tab, and select Run. Make sure your Formbird URLs and credentials are correct and point to a running Formbird environment.
Congratulations! You have setup a REST endpoint to integrate with Formbird!