Calling adapters in “desktop” does not work

2019-08-25 03:33发布

问题:

Using IBM MobileFirst 7.1

I have created a very simple hybrid project. Then added a very simple java adapter:

@Path("/hello")
public class BrokenAdapterResource {
    @GET
    @Produces("text/plain")
    public String hello() {
        return "Hope keeps man alive....!";
    }
}

and also created a MobileFirst environment: "Desktop Browser web page". I try to call the adapter. When I call it from the "common" it works as expected. However when I call it from "desktop" it does not work:

var resourceRequest = new WLResourceRequest(
        "/adapters/BrokenAdapter/hello",
        WLResourceRequest.GET);



resourceRequest.send().then(
        function(result){
            var data = JSON.parse(JSON.stringify(result))
            var message = data['responseText']
            $("#message").text(message) //Set the value in message div tag              
        },
        function(error){
            $("#message").text(JSON.stringify(error))
        }
    );

In "common" I get the expected: Hope keeps man alive....! . In the "desktop" I always get {"status":200,"responseHeaders":{},"responseText":"undefined","errorCode":"API_INVOCATION_FAILURE","errorMsg":"Unsupported environment","invocationContext":null}

I wonder what if wrong in this case? Is this the right way to call resources?

I have created a sample project that shows this thing https://hub.jazz.net/project/ignacio4d/BrokenProject/overview

回答1:

Please see the following tutorial: https://developer.ibm.com/mobilefirstplatform/documentation/getting-started-7-1/foundation/server-side-development-category/invoking-adapter-procedures-hybrid-client-applications/

specifically:

MobileFirst applications can access resources using the WLResourceRequest REST API. The REST API works with all adapters and external resources, and is supported in the following hybrid environments: iOS, Android, Windows Phone 8, and Windows 8.

If your application supports other hybrid environments such as BlackBerry, Mobile Web, or Desktop Browser, see the tutorial for IBM MobileFirst Platform Foundation 6.3.

In other words, Java adapters use WLResourceRequest. Java adapters do not support the Desktop Browser environment. A workaround would be to use JavaScript adapters which support both WL.Client.invokeProceudre and WLResourceRequest, so you can use regular WL.Client.invokeProcedure for the Desktop Browser environment.