Soap MobileFirst 6.3调用后端服务创建的适配器过程

Soap MobileFirst 6.3调用后端服务创建的适配器过程,soap,ibm-mobilefirst,mobilefirst-adapters,Soap,Ibm Mobilefirst,Mobilefirst Adapters,我很难想出如何调用MobileFirst使用Discover后端服务功能为我创建的适配器 我创建了适配器,但没有使用以下代码尝试调用它: function callService(){ alert("Calling into Backend"); var options = { onSuccess : loadSuccess, onFailure : loadFailure, invocationContex

我很难想出如何调用MobileFirst使用Discover后端服务功能为我创建的适配器

我创建了适配器,但没有使用以下代码尝试调用它:

function callService(){
    alert("Calling into Backend");
    var options = {
            onSuccess : loadSuccess,
            onFailure : loadFailure,
            invocationContext: {}
        };

    var params = {"itemRequiredReference":10};

    var invocationData = {
            adapter : 'SoapAdapter1',
            procedure : 'inquireSingleService_inquireSingle',
            parameters : [params]
        };

    WL.Client.invokeProcedure(invocationData, options);
}
但这似乎不起作用。我得到一个错误:

{"status":200,"invocationContext":{},"invocationResult":{"statusCode":500,"errors":[],"isSuccessful":true,"Envelope":{"SOAP-ENV":"http://schemas.xmlsoap.org/soap/envelope/","Body":{"Fault":{"":"","detail":{"CICSFault":{"CDATA":"DFHPI1010 02/02/2015 22:11:37 GENAPPA1 CPIH 00518 XML generation failed. A conversion error (INVALID_ZONED_DEC) occurred when converting field itemReferenceNumber for WEBSERVICE inquireSingleWrapper.","xmlns":"http://www.ibm.com/software/htp/cics/WSFault"}},"faultcode":"SOAP-ENV:Server","faultstring":"Conversion to SOAP failed"}},"soap":"http://schemas.xmlsoap.org/soap/envelope/"},"statusReason":"Internal Server Error","responseHeaders":{"Date":"Mon, 02 Feb 2015 22:11:37 GMT","Content-Length":"000000000000608","Content-Type":"text/xml; charset=UTF-8","Connection":"Keep-Alive","Server":"IBM_CICS_Transaction_Server/5.2.0(zOS)"},"warnings":[],"responseTime":578,"totalTime":581,"info":[]}}
生成的适配器代码如下所示:

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Generated code - Do not edit                                                                                  //
//                                                                                                               //
// This is a SOAP adapter that was auto-generated by Worklight for invocation of specific SOAP-based services.   //
// The adapter may invoke more than one service as long as they are all from the same enpdpoint (server host).   //
// Each adapter procedure matches a single operation for the same endpoint server and accepts:                   //
//   params  - Serialized JSON representation of the XML-based SOAP body to be sent to the service               //
//   headers - Custom HTTP headers to be specified when invoking the remote service. It is a JSON object with    //
//             the headers names and values. E.g. { 'name1' : 'value1', 'name2' : 'value2' }                     //
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function inquireSingleService_inquireSingle(params, headers){
    var soapEnvNS = '';
    // The value of 'soapEnvNS' was set based on the version of the SOAP to be used (i.e. 1.1 or 1.2).
    soapEnvNS = 'http://schemas.xmlsoap.org/soap/envelope/';

    // The following mappings object was autogenerated from the XML schema of the input message to the service.
    // It is being used to support a params JSON when invoking this procedure that don't specify the namespace
    // prefix nor specifying whether a property is attribute or not.
    // 
    // The 'roots' object has the list of message parts within the invocation SOAP message. Each entry has a
    // mapping between the root element name and its namespace prefix and type.
    // Each root object may define 'nsPrefix' and 'type'. Both are optional - If there is no need for a NS prefix
    // then the 'nsPrefix' should not be specified. If the element is a simple type then the 'type' should not be
    // specified.
    //
    // The 'types' object has a list of types each defining the children of the type and the definition of each
    // child. If the child is a complext type, the 'type' property has a reference to the child type definition.
    // Each child object may define:
    // 'nsPrefix' (optional) - Holds the namespace prefix to be attached to the element. If there is no need for 
    //   a NS prefix then the 'nsPrefix' should not be specified. 
    // 'type' (optional) - If the element is a simple type then the 'type' should not be specified. If it is an 
    //   attribute then 'type' should have the value of '@'. Otherwise the value of 'type' is a reference to the 
    //   type definition within the 'types' object.
    var mappings = {
        roots: {
            'inquireSingleRequest': { nsPrefix: 'reqns', type: 'reqns:inquireSingleRequest' }               
        },

        types: {
            'reqns:inquireSingleRequest': {
                children: [
                    {'itemRequiredReference': { nsPrefix: 'reqns' }}    
                ]
            }
        }
    };
    var namespaces = 'xmlns:resns="http://www.exampleApp.inquireSingleResponse.com" xmlns:reqns="http://www.exampleApp.inquireSingleRequest.com" xmlns:tns="http://www.exampleApp.inquireSingleResponse.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ';
    var request = buildBody(params, namespaces, mappings, soapEnvNS);
    var soapAction = '';
    return invokeWebService(request, headers, soapAction);
}



function buildBody(params, namespaces, mappings, soapEnvNS){
    var body =
        '<soap:Envelope xmlns:soap="' + soapEnvNS + '">\n' +
        '<soap:Body>\n';

    var fixedParams = {};
    for (var paramName in params) {
        if (mappings['roots'][paramName]) { //There is mapping for this param
            var root = mappings['roots'][paramName];
            var name = paramName;
            if (root['nsPrefix'])
                name = root['nsPrefix'] + ':' + paramName;
            fixedParams[name] = handleMappings(params[paramName], root['type'], mappings['types']); 
        }
        else {
            fixedParams[paramName] = params[paramName];
        }
    }

    body = jsonToXml(fixedParams, body, namespaces);

    body += 
        '</soap:Body>\n' +
        '</soap:Envelope>\n';
    return body;
}

function handleMappings(jsonObj, type, mappings) {
    var fixedObj = {};
    var typeMap = mappings[type]['children']; //Get the object that defines the mappings for the specific type

    // loop through the types and see if there is an input param defined
    for(var i = 0; i < typeMap.length; i++) {
        var childType = typeMap[i];

        for(var key in childType) {
            if(jsonObj[key] !== null) { // input param exists
                var childName = key;
                if (childType[key]['nsPrefix'])
                    childName = childType[key]['nsPrefix'] + ':' + key;

                if (!childType[key]['type']) //Simple type element
                    fixedObj[childName] = jsonObj[key];
                else if (typeof jsonObj[key] === 'object' && jsonObj[key].length != undefined) { //Array of complex type elements
                    fixedObj[childName] = [];
                    for (var i=0; i<jsonObj[key].length; i++)
                        fixedObj[childName][i] = handleMappings(jsonObj[key][i], childType[key]['type'], mappings);
                }
                else if (typeof jsonObj[key] === 'object') //Complex type element
                    fixedObj[childName] = handleMappings(jsonObj[key], childType[key]['type'], mappings);
                else if (childType[key]['type'] == '@') //Attribute
                    fixedObj['@' + childName] = jsonObj[key];
            }
        }
    }

    return fixedObj;
}

function getAttributes(jsonObj) {
    var attrStr = '';
    for(var attr in jsonObj) {
        if (attr.charAt(0) == '@') {
            var val = jsonObj[attr];
            attrStr += ' ' + attr.substring(1);
            attrStr += '="' + xmlEscape(val) + '"';
        }
    }
    return attrStr;
}

function jsonToXml(jsonObj, xmlStr, namespaces) {
    var toAppend = '';
    for(var attr in jsonObj) {
        if (attr.charAt(0) != '@') {
            var val = jsonObj[attr];
            if (typeof val  === 'object'  &&  val.length != undefined) {
                for(var i=0; i<val.length; i++) {
                    toAppend += "<" + attr + getAttributes(val[i]);
                    if (namespaces != null)
                        toAppend += ' ' + namespaces;
                    toAppend += ">\n";
                    toAppend = jsonToXml(val[i], toAppend);
                    toAppend += "</" + attr + ">\n";
                }
            }
            else {
                toAppend += "<" + attr;
                if (typeof val  === 'object') {
                    toAppend += getAttributes(val);
                    if (namespaces != null)
                        toAppend += ' ' + namespaces;
                    toAppend += ">\n";
                    toAppend = jsonToXml(val, toAppend);
                }
                else {
                    toAppend += ">" + xmlEscape(val);
                }
                toAppend += "</" + attr + ">\n";
            }
        }
    }
    return xmlStr += toAppend;
}


function invokeWebService(body, headers, soapAction){
    var input = {
        method : 'post',
        returnedContentType : 'xml',
        path : '/exampleApp/inquireSingleWrapper',
        body: {
            content : body.toString(),
            contentType : 'text/xml; charset=utf-8'
        }
    };
    WL.Logger.error("ANDY:"+body.toString());
    //Adding custom HTTP headers if they were provided as parameter to the procedure call
    //Always add header for SOAP action 
    headers = headers || {};
    if (soapAction != 'null')
        headers.SOAPAction = soapAction;
    input['headers'] = headers;

    return WL.Server.invokeHttp(input);
}

function xmlEscape(obj) {
    if(typeof obj !== 'string') {
        return obj;
    }
    return obj.replace(/&/g, '&amp;')
           .replace(/"/g, '&quot;')
           .replace(/'/g, '&apos;')
           .replace(/</g, '&lt;')
           .replace(/>/g, '&gt;');
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//生成的代码-不编辑//
//                                                                                                               //
//这是Worklight自动生成的SOAP适配器,用于调用特定的基于SOAP的服务//
//适配器可以调用多个服务,只要它们都来自同一个enpdpoint(服务器主机)//
//每个适配器过程都匹配同一端点服务器的单个操作,并接受://
//params—要发送到服务的基于XML的SOAP主体的序列化JSON表示//
//headers—调用远程服务时要指定的自定义HTTP头。它是一个JSON对象,具有//
//标题包含名称和值。例如{'name1':'value1','name2':'value2'}//
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
函数inquiresSingleService_inquiresSingle(参数、头){
var soapEnvNS='';
//“soapEnvNS”的值是根据要使用的SOAP版本(即1.1或1.2)设置的。
索本恩斯酒店http://schemas.xmlsoap.org/soap/envelope/';
//以下映射对象是从输入消息的XML架构自动生成到服务的。
//在调用此过程时,它被用来支持params JSON,而该过程不指定名称空间
//前缀或指定属性是否为属性。
// 
//“根”对象具有调用SOAP消息中的消息部分列表
//根元素名称与其命名空间前缀和类型之间的映射。
//每个根对象都可以定义“nsPrefix”和“type”。如果不需要NS前缀,则两者都是可选的
//则不应指定“nsPrefix”。如果元素是简单类型,则不应指定“type”
//指定的。
//
//“types”对象有一个类型列表,每个类型定义该类型的子级以及每个子级的定义
//如果子类型是complext类型,“type”属性引用子类型定义。
//每个子对象可以定义:
//“nsPrefix”(可选)-保留要附加到元素的命名空间前缀。如果不需要
//NS前缀,则不应指定“nsPrefix”。
//“类型”(可选)-如果元素是简单类型,则不应指定“类型”。如果是
//属性,则“type”的值应为“@”。否则,“type”的值是对
//“类型”对象中的类型定义。
变量映射={
根:{
'inquireSingleRequest':{nsPrefix:'reqns',键入:'reqns:inquireSingleRequest'}
},
类型:{
“请求:请求”:{
儿童:[
{'itemRequiredReference':{nsPrefix:'reqns'}
]
}
}
};
变量名称空间='xmlns:resns='http://www.exampleApp.inquireSingleResponse.com“xmlns:reqns=”http://www.exampleApp.inquireSingleRequest.com“xmlns:tns=”http://www.exampleApp.inquireSingleResponse.com“xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance" ';
var request=buildBody(参数、名称空间、映射、soapEnvNS);
var soapAction='';
返回invokeWebService(请求、头、soapAction);
}
函数构建体(参数、名称空间、映射、soapEnvNS){
变位体=
“\n”+
“\n”;
var fixedParams={};
for(参数中的变量paramName){
if(mappings['roots'][paramName]){//此参数存在映射
var root=mappings['roots'][paramName];
var name=paramName;
if(根['nsPrefix'])
name=root['nsPrefix']+':'+paramName;
fixedParams[name]=handleMappings(params[paramName],根['type'],映射['types']);
}
否则{
fixedParams[paramName]=params[paramName];
}
}
body=jsonToXml(fixedParams、body、名称空间);
正文+=
“\n”+
“\n”;
返回体;
}
函数handleMappings(jsonObj、类型、映射){
var fixedObj={};
var typeMap=mappings[type]['children'];//获取为特定类型定义映射的对象
//循环遍历这些类型,查看是否定义了输入参数
对于(变量i=0;i对于(var i=0;i我终于在这里找到了一个有用的文档页面:

解决方案是将变量参数设置为:

var params = {"inquireSingleRequest": {"itemRequiredReference": "10"}}

    var invocationData = {
            adapter : 'SoapAdapter1',
            procedure : 'inquireSingleService_inquireSingle',
            parameters : [params]
        };

这使它能够完美地工作。

使用SoapUI时它工作正常吗?现在下载它-但该服务在您的电脑上确实工作正常