How to Call RESTful Web Services from Groovy Scripts

You can call RESTful web services from your Groovy scripts in Application Composer. You might call a web service for access to internal or external data, or to perform a calculation on your data.

Calling RESTful web service methods from your scripts involves two high-level steps:

  1. Creating a reference to the web service.

    This includes registering the web service's endpoint with a variable name that you use in your Groovy script.

  2. Writing a Groovy script in the Expression Builder that calls the web service.

    For each call, the script must prepare the inbound arguments to the web service, call a web service method, and then process the return value from the web service.

Note: If the web service returns an error, you can use initiators like setAttribute() instead of web services inside the Groovy script.

Creating a Web Service Reference

To register a web service for use in your scripts, you first select Web Services in the Common Setup pane in Application Composer. You can select either REST or SOAP. To register REST services, select REST. You then associate a web service variable name with a URL that provides the location of the resource that represents the service you want to call.

For example, you might register a web service variable name of TwitterSearch for a web service that your application needs to call for retrieving tweets from Twitter, in this case, about Yosemite. The URL for this web service's location might be:

https://api.twitter.com/1.1/search/tweets.json?q=yosemite

Or, you might want to use this feature with an internal web service, such as within your cloud service. In this case, the URL might be:

http://host:port/OpptyService/rest/v1/Oppty?q=OpptyId=##OpptyId##

Of course, the server name, the port number, and path name for your actual service will be different. If the port number is omitted, then it's assumed that the service is listening on the default HTTP port number 80.

Writing a Groovy Script to Call a Web Service

When you call a web service from a Groovy script, the script must prepare the arguments to the web service before calling a web service method, and then process the data returned from the web service. Your script can also pass a structured payload to and from a web service.

You insert the code for the call to the web service from the Web Services tab in Expression Builder. The Web Services list displays the set of registered web service variable names and the Functions list displays the available methods for a given web service.

To insert a call to a RESTful web service in a Groovy script:

  1. Select the Web Services tab in Expression Builder.

  2. Select REST.

  3. Select a variable name from the Web Services list.

  4. Select a method from the Functions list.

    The code that will be inserted is shown under Function Signature.

    The information under Function Signature includes the parameter types and also the return type to indicate the type of variable the result of the call should be assigned to. The possible return types are:

    Return Value

    Return Type

    Void

    Void

    Scalar values (integer, string and so on)

    The actual Java return type

    Object

    Map

    Collection

    List

  5. Position the cursor at the place in the script where you want to insert the web service call.

  6. Click the Insert button to insert the code to call the web service method.

    A web service call from a Groovy script has the following syntax:

    adf.webServices.YourServiceVariableName.MethodName(args)

    For example:

    adf.webServices.ContactAddressAPI.Get("7627")
  7. Click Submit.