2017-08-31 89 views
1

我在WSO2 ESB 5.0中实现了一个应接受带有XML主体的POST的API,然后将其转发给另一个带有包含原始XML主体的JSON主体的webservice属性。WSO2 ESB:将原始XML正文包含到JSON负载中

例子:

我发布了以下机身到我的ESB API:

<?xml version="1.0" encoding="utf-8"?> 
<workorder id="foobar"> 
    <foo/> 
</workorder> 

我希望类似以下内容被张贴到我的web服务:现在

{ 
    "key1": "value1", 
    "key2": "value2", 
    "input" : "<?xml version=\"1.0\" encoding=\"utf-8\"?><workorder id=\"foobar\"><foo/></workorder>" 
} 

对于inSequence的样子:

<inSequence> 
     <property name="messageType" value="application/xml" scope="axis2"/> 
     <log level="full"/> 
     <enrich description="Store workorder"> 
      <source type="body" clone="true"/> 
      <target type="property" property="SENT_WORKORDER"/> 
     </enrich> 
     <payloadFactory media-type="json" description=""> 
      <format>{"key1": "value1", "key2": "value2", "input": "$ctx:SENT_WORKORDER"}</format> 
      <args/> 
     </payloadFactory> 
     <log level="full"/> 
     <property name="REST_URL_POSTFIX" value="/my/service" scope="axis2" type="STRING" description="Set URL"/> 
     <send> 
      <endpoint key="conf:/endpoints/my_endpoint"/> 
     </send> 
</inSequence> 

它返回:

{ 
    "key1": "value1", 
    "key2": "value2", 
    "input" : "[<workorder id="foobar"><foo/></workorder>]" 
} 

我不知道如何着手。我想要的只是检索我发布的原始文本(并且转义双引号以便它可以包含在JSON中)。

回答

0

我结束了使用脚本中介来处理这个问题:

<inSequence> 
     <log level="full"/> 

     <script language="js"><![CDATA[ 
     var log = mc.getServiceLog(); 
     var payload = {"key1": "value1", "key2": "value2", "input": ""}; 
     payload["input"] = mc.getPayloadXML().toString(); 
     log.info("Built payload: " + JSON.stringify(payload)); 
     mc.setPayloadJSON(JSON.stringify(payload)); 
     ]]></script> 

     <property name="REST_URL_POSTFIX" value="/my/service" scope="axis2" type="STRING" description="Set URL"/> 
     <property description="Set Content-Type" name="messageType" scope="axis2" type="STRING" value="application/json"/> 

     <send> 
      <endpoint key="conf:/endpoints/my_endpoint"/> 
     </send> 
</inSequence> 

出于某种原因,JSON.stringify通话以及最后的“设置内容类型”是必需的。作为奖励,我还需要做与我在这里问的相反的内容:从JSON负载属性中提取XML并将其作为正确的XML对象返回。 该序列的相关部分为:

<script language="js"><![CDATA[ 
var payload = mc.getPayloadJSON(); 
var report = payload.path.to.xml.attribute; 
mc.setPayloadXML(XML(report)); 
]]></script> 

<property description="Set Content-Type" name="messageType" scope="axis2" type="STRING" value="application/xml"/>