简体   繁体   English

使用带参数的外部服务的WSO2混搭

[英]WSO2 Mashup using external services with arguments

I'm trying to create a mashup in WSO2 AS 5.1.0. 我正在尝试在WSO2 AS 5.1.0中创建一个混搭。 I have managed to succesfully create a simple HelloWorld service, however when I try to integrate other service into it, I get an error. 我已经成功创建了一个简单的HelloWorld服务,但是当我尝试将其他服务集成到其中时,出现了错误。

This is my HelloWorld service: 这是我的HelloWorld服务:

this.documentation = "This is a test Hello World service";
system.include("HelloStub.js");

hello.documentation = "say hello"
hello.inputTypes = {"user": "string"}
hello.outputType = "string";
function hello(user){
    try{
        var response = services["admin/testmashup"].operations["sayMyName"](user);
    }catch(e){
        return "Danger, Robinson! " + e.toString()
    }
    return "Hello, there! " + response;
}

function whoAreYou(){
    try{
        var response = services["admin/testmashup"].operations["toString"]();
    }catch(e){
        return "Danger, Robinson! " + e.toString()
    }
    return "Hello! " + response;    
}

And this is the admin/testmashup service 这是admin/testmashup服务

this.serviceName = "testmashup";
this.documentation = "Test mashup service" ;

toString.documentation = "say something" ;
toString.inputTypes = { /* no arguments */ };
toString.outputType = "string"; 
function toString()
{
   return "Hi, my name is testmashup";
}

sayMyName.documentation = "Make me feel happy";
sayMyName.inputTypes = {"myName":"string"};
sayMyName.outputType = "string";
function sayMyName(myName){
    return "Your very beautiful name is " + myName;
}

I must note that when I call the admin/testmashup service, it works as expected. 我必须注意,当我调用admin/testmashup服务时,它会按预期工作。

The file HelloStub.js is a Javascript (E4X) stub, generated by the WSO2 Applicanton Server. 文件HelloStub.js是由WSO2 Applicanton Server生成的Javascript(E4X)存根。

When I test the operation whoAreYou , which has no arguments, I get the following response: 当我测试没有参数的whoAreYou操作whoAreYou ,得到以下响应:

<ws:whoAreYouResponse xmlns:ws="http://services.mashup.wso2.org/helloWorld?xsd">
   <return xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:js="http://www.wso2.org/ns/jstype" js:type="string" xsi:type="xs:string">Hello! &lt;ws:toStringResponse xmlns:ws="http://services.mashup.wso2.org/testmashup?xsd"&gt;&lt;return&gt;Hi, my name is testmashup&lt;/return&gt;&lt;/ws:toStringResponse&gt;</return>
</ws:whoAreYouResponse>

I can see the text Hi, my name is testmashup within the encoded response. 我可以看到文本Hi, my name is testmashup编码响应中的Hi, my name is testmashup But when I try to call hello , with the following xml: 但是,当我尝试使用以下xml调用hello

<body>
   <p:hello xmlns:p="http://services.mashup.wso2.org/helloWorld?xsd">
      <!--Exactly 1 occurrence-->
      <user>John</user>
   </p:hello>
</body>

I get the following error: 我收到以下错误:

<ws:helloResponse xmlns:ws="http://services.mashup.wso2.org/helloWorld?xsd">
   <return>Danger, Robinson! org.wso2.carbon.CarbonException: Invalid input for the payload in WSRequest Hostobject : John</return>
</ws:helloResponse>

I have tried to make this work for the last couple of days and have searched all over the place for an answer, but I can't seem to find it. 最近几天,我一直在努力使这项工作可行,并在各地搜索了答案,但似乎找不到。 The official documentation does not provide an example using stubs of external webservices that have operations with one or more arguments. 官方文档没有提供使用带有一个或多个参数的操作的外部Web服务存根的示例。

Also, if it is possible, I would like to know how to consume REST-JSON services from a javascript mashup. 另外,如果可能的话,我想知道如何从javascript mashup使用REST-JSON服务。

Any ideas? 有任何想法吗?

It turns out there is an important bunch of information missing in the official documentation. 事实证明,官方文档中缺少大量重要信息。 In particular, regading my issue, it is important to know that internally the generated stub uses BagderFish notation to perform the conversion between XML and JSON. 特别地,回想一下我的问题,了解内部生成的存根使用BagderFish表示法在XML和JSON之间进行转换非常重要。 I went into the stub and dived a bit and foud this piece of code, concerning to my sayMyName method: 我进入了存根,深入了一下,发现这段代码与我的sayMyName方法有关:

service.operations['sayMyName'] = function (request) {
    var isAsync, response, resultValue;
    var operation = service.operations['sayMyName'];
    request = typeof request === "string" ? request : utils.bf2xml(request);
    service.$._options = new Array();
    .
    .
    .

The function bf2xml converts JSON in BadgerFish notation to XML. 函数bf2xml将BadgerFish表示法中的JSON转换为XML。 The function is defined later in the stub, and can be found here too. 该函数稍后在存根中定义,也可以在此处找到。 Information on BadgerFish notation is widely available also. 关于BadgerFish符号的信息也很广泛。

In order to make the HelloWorld service work properly, you can pass either a string with the XML (in that case the bf2xml function will not be called), or a JSON object with the correct notation, ie, BadgerFish. 为了使HelloWorld服务正常工作,您可以传递带有XML的字符串(在这种情况下,将不会调用bf2xml函数),或者传递具有正确表示法的JSON对象,即BadgerFish。

This will work: 这将起作用:

var response = services["admin/testmashup"].operations["sayMyName"]({request : {myName:{$:user}}});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM