Class Cast: java.lang.String cannot be cast to org.mozilla.javascript.Scriptable

ぃ、小莉子 提交于 2019-12-24 12:43:35

问题


While invoking a http adapter procedure, it popsup a dialog with ProcedureName, Signature and Paramaters and when I hit Run button after entering two string type parameters, I am getting "Class Cast: java.lang.String cannot be cast to org.mozilla.javascript.Scriptable" error.

FYI, I created a worklight adapter using worklight application framework data object editor(automatically generates .xml and impl.js files)

impl.js file

function CurrencyConvertor_ConversionRate(params, headers){
    var soapEnvNS;

    soapEnvNS = 'http://schemas.xmlsoap.org/soap/envelope/';
    var request = buildBody(params, 'xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://www.webserviceX.NET/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" ', soapEnvNS);
    return invokeWebService(request, headers);
}



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

    body = jsonToXml(params, body, namespaces);

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

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

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


function invokeWebService(body, headers){
    var input = {
        method : 'post',
        returnedContentType : 'xml',
        path : '/CurrencyConvertor.asmx',
        body: {
            content : body.toString(),
            contentType : 'text/xml; charset=utf-8'
        }
    };

    //Adding custom HTTP headers if they were provided as parameter to the procedure call 
    headers && (input['headers'] = headers);

    return WL.Server.invokeHttp(input);
}

回答1:


The error indicates that there is an invalid JSON object somewhere in your code.

Most probably this error raised while converting the body to String using body.toString() as toString will return [object Object] which is invalid JSON object value (neither valid String nor valid Array)

use json.stringify(body) instead, it should make what you intended to do.

besides, try to add some log lines to ease tracing the error



来源:https://stackoverflow.com/questions/23192346/class-cast-java-lang-string-cannot-be-cast-to-org-mozilla-javascript-scriptable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!