【问题标题】:Fetch value of JSON response in BPEL variable获取 BPEL 变量中 JSON 响应的值
【发布时间】:2019-10-28 21:20:38
【问题描述】:

我正在尝试使用 javascript 在 SOA 12c BPEL assig 活动中以 JSON 格式获取休息服务返回的属性值。

rest服务返回的响应如下:

<messages>
<InputVar>
      <json>{

}</json>
   </InputVar>
<OutputVar>
      <json>{
   "response": [
      {
         "local_product_version": 1,
         "local_product_id": 1
      },
      {
         "local_product_version": 1,
         "local_product_id": 2
      },
      {
         "local_product_version": 1,
         "local_product_id": 3
      }
   ]
}</json>
   </OutputVar>
</messages>

当我使用分配活动来捕获响应属性值时,变量的值如下:


   <id xmlns:def="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="def:string">org.mozilla.javascript.Undefined@41c96386</id>


代替上面的org.mozilla.javascript.Undefined@41c96386,我需要的属性值如下图:

<id xmlns:def="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="def:string">1</id>

我已经尝试过使用以下 javascript 语法,但没有成功:

  1. OutputVar.message.data.response.local_product_version
    
  2. 2.
OutputVar.response[0].local_product_version

有人可以建议我如何实现同样的目标吗?

【问题讨论】:

    标签: javascript soa weblogic12c bpel


    【解决方案1】:

    为什么不“手动”从 json 响应中提取对象:

    const txt = `
    <messages>
    <InputVar>
          <json>{
    
    }</json>
       </InputVar>
    <OutputVar>
          <json>{
       "response": [
          {
             "local_product_version": 1,
             "local_product_id": 1
          },
          {
             "local_product_version": 1,
             "local_product_id": 2
          },
          {
             "local_product_version": 1,
             "local_product_id": 3
          }
       ]
    }</json>
       </OutputVar>
    </messages>
    `;
    
    const RE = /\<OutputVar\>\s*\<json\>(.*)\<\/json\>\s*\<\/OutputVar\>/s;
    
    const result = RE.exec(txt);
    
    if (result) {
      const json = result[1];
      const obj = JSON.parse(json)
      console.log(obj)
    }
    

    所以,你得到了:

    // {
    //   response: [
    //     { local_product_version: 1, local_product_id: 1 },
    //     { local_product_version: 1, local_product_id: 2 },
    //     { local_product_version: 1, local_product_id: 3 }
    //   ]
    // } 
    

    现在你把它作为一个普通的 JS 对象得到了,并且可以从中得到任何需要的东西,包括local_product_version

    然后你可以用它来制作想要的结果字符串:

    const local_product_version = obj.response[0].local_product_version;
    const result = `<id xmlns:def="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="def:string">${local_product_version}</id>`
    
    console.log(result);
    
    // <id xmlns:def="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="def:string">1</id>
    

    【讨论】:

    • 我需要在 SOA 中做,所以如果你能告诉我如何在分配活动中做它会更有帮助。
    • 我的意思是,如果你可以运行一些 JS,那么你就可以自己产生想要的结果。假设您需要一个看起来像 &lt;id xmlns:def="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="def:string"&gt;1&lt;/id&gt; 的字符串。然后你制作这样一个字符串,在里面插入正确的 local_product_version 值 - 代替 1
    • const local_product_version = ;常量结果 = &lt;id xmlns:def="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="def:string"&gt;${local_product_version}&lt;/id&gt;; - 现在你得到了你正在寻找的字符串。
    • 否则在SOA的特定环境下我不知道该怎么做,抱歉。
    • 大家好,我想在 JSON 数组中附加一组 JSON 对象,如何在 BPEL javascript 活动中做到这一点?谁能帮忙
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-08
    • 1970-01-01
    • 1970-01-01
    • 2019-08-24
    • 2019-02-22
    • 1970-01-01
    相关资源
    最近更新 更多