MobileFirst 7.1 - WL.Client.invokeProcedure和Java Adapter

时间:2017-09-05 14:24:50

标签: ibm-mobilefirst

我们使用的是MobileFirst 7.1.0.0-MFPF-IF201705051403版本。

我想知道WL.Client.invokeProcedure和Java Adapter组合是否有效?

我尝试使用WL.Client.invokeProcedure调用Java适配器,但它给了我一些错误。

Sample Project

Java Adapter

@Path("/users")
public class TestJavaResource {
    /* Path for method: "<server address>/CustomAuth-release71/adapters/TestJava/users/username" */
    @GET
    @Path("username")
    public String helloUser(){
        return "Hello Username" ;
    }   
}

在index.html中添加了2个按钮

<div>
        <input type="button" id="AuthSubmitButton" onclick="WLResourceRequestExample()" class="formButton" value="WLResourceRequestExample" />
        <input type="button" id="AuthCancelButton" onclick="invokeProcedureExample()" class="formButton" value="invokeProcedureExample" />
        </div>

main.js

function wlCommonInit(){

}

function invokeProcedureExample(){

    var invocationData = {
            adapter: "TestJava",
            procedure: "users/username",
            parameters: []
        };

        WL.Client.invokeProcedure(invocationData, {
            onSuccess : function(res){
                console.log(res);
            console.log('Success');
            },
            onFailure : function(res){
                console.log(res);
                console.log('Fail');},
            timeout : 30000
        });

}

function WLResourceRequestExample(){
    var resourceRequest = new WLResourceRequest("/adapters/TestJava/users/username", WLResourceRequest.GET, 30000);
    resourceRequest.send().then(
            function(res){
                console.log(res);
                console.log('Success')
                },
            function(res){
                    console.log(res);
                    console.log('Fail');
                    }
    );
}

通过WLResourceRequest调用Java Adapter时,它可以工作。

wlclient init started
    worklight.js:5384 before: initOptions.onSuccess
    worklight.js:5384 after: initOptions.onSuccess
    worklight.js:5384 wlclient init success
    test.js:1 test.js was run
    main.js:49 klass {responseHeaders: {…}, status: 200, responseText: "Hello Username", errorCode: undefined, errorMsg: "OK", …}
    main.js:50 Success
    worklight.js:5384 Request [/CustomAuth-release71/apps/services/api/HybridCustomAuth/common/query] 

通过invokeProcedure调用Java Adapter时会出现错误

Request [/CustomAuth-release71/apps/services/api/HybridCustomAuth/common/query]
worklight.js:1193 POST http://10.146.17.219:10080/CustomAuth-release71/apps/services/api/HybridCustomAuth/common/query 401 (Unauthorized)
request @ worklight.js:1193
initialize @ worklight.js:1151
klass @ worklight.js:540
__sendRequest @ worklight.js:3678
(anonymous) @ worklight.js:3661
(anonymous) @ wljq.js:1200
fire @ wljq.js:1045
add @ wljq.js:1091
(anonymous) @ wljq.js:1199
each @ wljq.js:656
(anonymous) @ wljq.js:1195
Deferred @ wljq.js:1256
then @ wljq.js:1194
sendRequest @ worklight.js:3656
initialize @ worklight.js:3522
klass @ worklight.js:540
__invokeProcedure @ worklight.js:9150
(anonymous) @ worklight.js:9193
setTimeout (async)
invokeProcedure @ worklight.js:9187
invokeProcedureExample @ main.js:32
onclick @ index.html:57
worklight.js:5384 Request [/CustomAuth-release71/apps/services/api/HybridCustomAuth/common/query]
worklight.js:5384 Procedure invocation error. Runtime: Procedure TestJava/users/username does not exist.
__log @ worklight.js:5384
PUBLIC_API.(anonymous function) @ worklight.js:5786
onInvokeProcedureSuccess @ worklight.js:9069
onSuccess @ worklight.js:3793
onWlSuccess @ worklight.js:3766
bound @ worklight.js:1029
respondToReadyState @ worklight.js:1287
onStateChange @ worklight.js:1209
bound @ worklight.js:1029
XMLHttpRequest.send (async)
request @ worklight.js:1193
initialize @ worklight.js:1151
klass @ worklight.js:540
__sendRequest @ worklight.js:3678
(anonymous) @ worklight.js:3661
(anonymous) @ wljq.js:1200
fire @ wljq.js:1045
add @ wljq.js:1091
(anonymous) @ wljq.js:1199
each @ wljq.js:656
(anonymous) @ wljq.js:1195
Deferred @ wljq.js:1256
then @ wljq.js:1194
sendRequest @ worklight.js:3656
handleResendOrSendPostAnswers @ worklight.js:4058
removeExpectedAnswer @ worklight.js:4027
challengeHandler.submitChallengeAnswer @ worklight.js:9888
wl_antiXSRFChallengeHandler.handleChallenge @ worklight.js:10655
AbstractChallengeHandler.startChallengeHandling @ worklight.js:9784
executeChallenge @ worklight.js:9690
checkResponseForChallenges @ worklight.js:9592
onWlFailure @ worklight.js:3830
bound @ worklight.js:1029
respondToReadyState @ worklight.js:1287
onStateChange @ worklight.js:1209
bound @ worklight.js:1029
XMLHttpRequest.send (async)
request @ worklight.js:1193
initialize @ worklight.js:1151
klass @ worklight.js:540
__sendRequest @ worklight.js:3678
(anonymous) @ worklight.js:3661
(anonymous) @ wljq.js:1200
fire @ wljq.js:1045
add @ wljq.js:1091
(anonymous) @ wljq.js:1199
each @ wljq.js:656
(anonymous) @ wljq.js:1195
Deferred @ wljq.js:1256
then @ wljq.js:1194
sendRequest @ worklight.js:3656
initialize @ worklight.js:3522
klass @ worklight.js:540
__invokeProcedure @ worklight.js:9150
(anonymous) @ worklight.js:9193
setTimeout (async)
invokeProcedure @ worklight.js:9187
invokeProcedureExample @ main.js:32
onclick @ index.html:57
main.js:38 klass {responseHeaders: {…}, status: 200, responseText: "/*-secure-↵{"WL-Authentication-Success":{"wl_antiX…],"isSuccessful":false,"warnings":[],"info":[]}*/", responseJSON: {…}, errorCode: "PROCEDURE_ERROR", …}
main.js:39 Fail
worklight.js:5384 Request [/CustomAuth-release71/apps/services/loguploader]
worklight.js:5384 Request [/CustomAuth-release71/apps/services/loguploader]
worklight.js:5983 Client logs successfully sent to the server
worklight.js:5983 Client logs successfully sent to the server

1 个答案:

答案 0 :(得分:0)

invokeProcedure API与JAVA Adapter不兼容,这就是您最终遇到错误的原因。它仅受JavaScript Adapter支持。

WLResourceRequest API支持JAVA和JavaScript适配器。