GroovyでのWebサービス・メソッドのコール
変数名YourServiceVariableNameに登録したwebサービスでsomeMethodName()という名前のWebサービス・メソッドを起動するには、次の構文を使用します:
adf.webServices.
YourServiceVariableName
.
someMethodName
(
args
)
たとえば、次の例は、webサービス変数名EmployeeService
に登録されたWebサービスでgetEmployee()
メソッドを起動し、整数7839
を単一の引数として関数に渡す方法を示しています。
// retrieve Employee object by id from remote system
def emp = adf.webServices.EmployeeService.getEmployee(7839)
// log a message, referencing employee fields with "dot" notation
println('Got employee '+emp.Ename+' with id '+emp.Empno)
// access the nested list of Dependent objects for this employee
def deps = emp.Dependents
if (deps != null) {
println("Found "+deps.size()+" dependents")
for (dep in deps) {
println("Dependent:"+dep.Name)
}
}
次の例のコードは、Groovyの便利なMap and List構造表記法を使用して、2つのネストされた依存を持つ新しい従業員を作成する方法を示しています。 その後、newEmp
オブジェクトは、サービスのcreateEmployee()
メソッドに引数として渡されます。
// Create a new employee object using a Groovy map. The
// nested collection of dependents is a Groovy list of maps
def newEmp = [ Ename:"Steve",
Deptno:10,
Job:"CLERK",
Sal:1234,
Dependents:[[Name:"Timmy",BirthYear:1996],
[Name:"Sally",BirthYear:1998]]]
// Create the new employee by passing this object to a web service
newEmp = adf.webServices.EmployeeService.createEmployee(newEmp)
// The service returns a new employee object which may have
// other attributes defaulted/assigned by the service, like the Empno
println("New employee created was assigned Empno = "+ newEmp.Empno)
この例のスクリプトは、mergeEmployee()
メソッドを使用して、getEmployee()
メソッドへの別のコールを介してアウト・セットで取得された従業員オブジェクトのフィールドを更新する方法を示しています。 このスクリプトは、取得したemp
オブジェクトのEname
フィールドを更新し、既存の依存の名前を更新してから、同じサービスでmergeEmployee()
メソッドをコールして変更を保存する前に、新しい依存を追加します。
// Merge updates and inserts on Employee and nested Dependents
def emp = adf.webServices.EmployeeService.getEmployee(7839)
// update employee's name to add an exclamation point!
emp.Ename = emp.Ename + '!'
def deps = emp.Dependents
// Update dependent names to add an exclamation point!
for (dep in deps) {
dep.Name = dep.Name + '!'
}
// Add a new dependent
def newChild = [Name:"Jane", BirthYear:1997]
deps.add(newChild)
emp = adf.webServices.EmployeeService.mergeEmployee(emp)
base64エンコード形式でデータを予期または返すwebサービスを起動する必要がある場合は、「その他の組込みGroovy関数の理解」で説明されているbase64-relatedヘルパー関数を使用できます。