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 Files
section of this documentation. Specify your actual Formbird URLs and credentials during setup in order to run the Job within Talend Studio. -
Add a
tRESTRequest
component -
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
tJavaRow
component to theJob
, and copy the following code to it. -
Under the component's
Advanced settings
panel, 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 settings
panel, 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
tRunJob
component and select a sub Job to run. SelectTransmit whole context
to pass context values to the sub Job. DeselectDie on child error
. -
Add a
tJava
component, and copy the following code to it. -
Under the component's
Advanced settings
panel, add:java import org.apache.log4j.Logger;
-
Under the component's
Basic settings
panel, add:java Logger logger = Logger.getLogger(context.loggerName); String method = jobName + "." + currentComponent; logger.info(method + " onSubjobOk");
-
Add a
tJavaRow
component, and copy the following code to it. -
Under the component's
Advanced settings
panel, add:```java import org.apache.log4j.Logger;
import com.fieldtec.webclient.model.Document; import com.fieldtec.webclient.util.UtilJson; ```
-
Under the component's
Basic settings
panel, 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
tRESTResponse
component and configure as follows: -
Return Body Type: String
- Return status code: OK (200)
-
Pass
body
as the schema -
Connect the
tRESTRequest
component [step 2] to thetJavaRow
component [step 5] using aMain
Row link. Passbody
as the schema. -
Connect the
tJavaRow
component [step 5] to thetRunJob
component [step 6] using anRun If
trigger, and specify the trigger as: "context.requestDocument != null". -
Connect the
tRunJob
component [step 6] to thetJava
component [step 7] using anOn Subjob Ok
trigger. -
Connect the
tJava
component [step 7] to thetJavaRow
component [step 8] using aMain
Row link. Passbody
as the schema. -
Connect the
tJavaRow
component [step 8] to thetRESTResponse
component [step 9] using aMain
Row link. Passbody
as 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 Files
section 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
tJava
component to theJob
, and copy the following code to it. -
Under the component's
Advanced settings
panel, 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 settings
panel, 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!