PUT HTTP Adapter in MobileFirst Platform

2019-09-08 03:39发布

问题:

I am trying to post some data from my native android application.

Native Code:

WLProcedureInvocationData invocationData = new WLProcedureInvocationData("TaskAdapter", "updateTask");

int taskId = Integer.parseInt(tvTaskId.getText().toString());

String assignedTo = tvAssignedTo.getText().toString();

String address = "";

String description = "";

String latitude = "5.0";

String longitude = "5.0";

String status = "5.0";
String comments = "5.0";
String lastupdate = "5.0";
String userLatitude = "5.0";
String userLongitude = "5.0";
String userLocation = "5.0";
String photoData = "5.0";

Object[] parameters = new Object[]{
    taskId,
    assignedTo,
    description,
    address,
    latitude,
    longitude,
    status,
    comments,
    lastupdate,
    userLatitude,
    userLongitude,
    userLocation,
    photoData
};

invocationData.setParameters(parameters);

WLRequestOptions options = new WLRequestOptions();
options.setTimeout(30000);


client.getInstance().invokeProcedure(invocationData, new MyInvokeListener(), options);

Adapter Code:

function updateTask(id) {   
    var input = {
        method : 'PUT',
        returnedContentType : 'json',
        path : '/Api/Task?taskid=' + id
    };


    return WL.Server.invokeHttp(input);
}

Adapter XML:

<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed Materials - Property of IBM
5725-I43 (C) Copyright IBM Corp. 2011, 2013. All Rights Reserved.
US Government Users Restricted Rights - Use, duplication or
disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
-->
<wl:adapter name="TaskAdapter" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:wl="http://www.ibm.com/mfp/integration" xmlns:http="http://www.ibm.com/mfp/integration/http">

    <displayName>TaskAdapter</displayName>
    <description>TaskAdapter</description>
    <connectivity>
        <connectionPolicy xsi:type="http:HTTPConnectionPolicyType">
            <protocol>http</protocol>
            <domain>testmeternative.vdot.virginia.gov</domain>
            <port>80</port>
            <connectionTimeoutInMilliseconds>30000</connectionTimeoutInMilliseconds>
            <socketTimeoutInMilliseconds>30000</socketTimeoutInMilliseconds>
            <maxConcurrentConnectionsPerNode>50</maxConcurrentConnectionsPerNode>

            <!-- Following properties used by adapter's key manager for choosing specific 
                certificate from key store <sslCertificateAlias></sslCertificateAlias> <sslCertificatePassword></sslCertificatePassword> -->
        </connectionPolicy>
    </connectivity>

    <procedure name="getAllTasks" />
    <procedure name="updateTask" />


</wl:adapter>

I am not sure whether I am sending body in the right way. Moreover, how do I send the id (parameter) to the adapter function.

When I click Call Mobile First Adapter in eclipse, it is showing me the procedure name but the REST call type as GET in the drop down, I want it as PUT.

回答1:

You will need to update your adapter code as follow:

function updateTask(id, assignedTo, description, address, latitude, longitude,
        status, comments, lastupdate, userLatitude, userLongitude,
        userLocation, photoData) {

    var data = {
        "assignedTo" : assignedTo,
        "description" : description,
        "address" : address,
        "latitude" : latitude,
        "longitude" : longitude,
        "status" : status,
        "comments" : comments,
        "lastupdate" : lastupdate,
        "userLatitude" : userLongitude,
        "userLocation" : userLocation,
        "photoData" : photoData
    };

    var input = {
        method : 'PUT',
        returnedContentType : 'json',
        path : '/Api/Task?taskid=' + id,
        body : {
            contentType : 'application/json',
            content : data
        }
    };

    return WL.Server.invokeHttp(input);
}

Since you are passing the values to the adapter via invocationData.setParameters(parameters); in your native code that means the adapter will take that same number of parameters in the same order.

I created an object data that will contain all these values except the id or taskId since you are passing it as a query param. Then I'm assuming your back-end services accepts a Content-Type of application/json, you can change the content-type if need be.



回答2:

Make sure you distinguish between how your app invokes the adapter and how the adapter invokes the backend - these are separate concepts.

In earlier versions of MFP/Worklight adapters were invoked use HTTP GET; the adapters themselves may have then invoked the backend with GET, PUT or POST, but the app was effectively making an RPC call over HTTP.

With MFP version 7.0 we gain the capability to create RESTful adapter, which can be invoked using GET, PUT, POST or DELETE. Those adapters are implemented in Java using the JAX/RS programming model. Each individual procedure will be marked as using one of those HTTP "verbs", and then when you test in Eclipse as you select the procedure the appropriate GET/PUT/POST is offered. In your example you have a simple traditional JavaScript adapter and so can only use GET and that's what is offered by the test tool.

To invoke a Java RESTful adapter, you do specify the verb. See this tutorial:

To see how to create a Java RESTful adapter see this tutorial