WSO2ESB: Property setting not accepting empty valu

2019-09-17 20:37发布

Actually I'm trying to get an empty value when I set a Property in a sequence in WSO2 ESB with an empty string. I have tried many things but always get the result "null" or "\"\"" instead of "" when I get the property, here is my code:

<property value="&quot;&quot;"
name="arq.general.DestinationSystem" scope="default"
type="STRING" xmlns:ns="http://org.apache.synapse/xsd" 
xmlns:ns2="http://org.apache.synapse/xsd"/>
<property name="arq.general.ParentInstanceID" scope="default"
type="STRING" value=""/>
<property expression="get-property('NonExistentProperty')" 
name="arq.functional.User"
scope="default" type="STRING"
xmlns:ns="http://org.apache.synapse/xsd" 
xmlns:ns2="http://org.apache.synapse/xsd"/>

Please could you help?

Cheers,

Tony

++ the payload Factory:

<payloadFactory media-type="xml">
            <format>
                <MensajeAuditoria xmlns="">
                    <Timestamp>$1</Timestamp>
                    <TrackingID>$2</TrackingID>
                    <SourceApplication>$3</SourceApplication>
                    <OperationName>$4</OperationName>
                    <ParentInstanceID>$5</ParentInstanceID>
                    <InstanceID>$6</InstanceID>
                    <ServiceID>$7</ServiceID>
                    <FunctionalID>$8</FunctionalID>
                    <AdapterType>$9</AdapterType>
                    <AdapterPoint>$10</AdapterPoint>
                    <HostName>$11</HostName>
                    <User>$12</User>
                </MensajeAuditoria>
            </format>
            <args>
                <arg evaluator="xml" expression="get-property('SYSTEM_TIME')"/>
                <arg evaluator="xml" expression="get-property('arq.general.TrackingID')"/>
                <arg evaluator="xml" expression="get-property('arq.general.SourceApplication')"/>
                <arg evaluator="xml" expression="get-property('arq.functional.OperationName')"/>
                <arg evaluator="xml" expression="get-property('arq.general.ParentInstanceID')"/>
                <arg evaluator="xml" expression="get-property('arq.general.InstanceID')"/>
                <arg evaluator="xml" expression="get-property('arq.general.ServiceID')"/>
                <arg evaluator="xml" expression="get-property('arq.functional.FunctionalID')"/>
                <arg evaluator="xml" expression="get-property('arq.general.AdapterType')"/>
                <arg evaluator="xml" expression="$func:AdapterPoint"/>
                <arg evaluator="xml" expression="get-property('SERVER_IP')"/>
                <arg evaluator="xml" expression="get-property('arq.functional.User')"/>

            </args>
        </payloadFactory>
    </else>
</filter>
<property name="messageType" scope="axis2" type="STRING" value="application/json"/>

Adding the factory result:

{
"MensajeAuditoria": {
    "Timestamp": 1492777451830,
    "TrackingID": "76b9858d-8421-4d7e-d2af-e8e411382e2e",
    "SourceApplication": "API Manager",
    "OperationName": null,
    "ParentInstanceID": null,
    "InstanceID": "76b9858d-8421-4d7e-d2af-e8e411382e2e",
    "ServiceID": "PRX_PROY1_AEX_AltaCliente",
    "FunctionalID": null,
    "AdapterType": "AEXP",
    "AdapterPoint": "PreActRequest",
    "HostName": "172.16.3.97",
    "User": null,
}}

4条回答
ら.Afraid
2楼-- · 2019-09-17 21:11

An other solution is to use "string" xpath function :

<property name="arq.general.ParentInstanceID" expression="string('')"/>
查看更多
你好瞎i
3楼-- · 2019-09-17 21:12

I have tried your payloadFactory and it turns out it works like a charm on 4.8.1 but it fails on 5.0.0. This is most likely a change in the JSON message builder that they use in the ESB. The problem is not on your empty property but in the automatic translation from XML to JSON that happens when you set the messageType property.

What you can do to solve this though is the make the payload mediator create json straight away as follows:

<payloadFactory media-type="json">
            <format>
               {"MensajeAuditoria":{
                    "Timestamp":$1,
                    "TrackingID":"$2"
                    ...
                }
               }

            </format>
            <args>
                <arg evaluator="xml" expression="get-property('SYSTEM_TIME')"/>
                <arg evaluator="xml" expression="get-property('arq.general.TrackingID')"/>
                ...
            </args>
        </payloadFactory>
    </else>
</filter>

This way you are in control of the quotes, and it will never put null there unless you actually want it there.

Hope this helps solve your problem

查看更多
相关推荐>>
4楼-- · 2019-09-17 21:15
Good thing would be to use enrich mediator to map the json field to xml field if there is a vale for the incoming field.
Sample
      <filter regex="true" source="boolean(get-property('START_DATE'))">
        <then>
          <enrich description="Add startDate tag">
            <source clone="true" type="inline">
              <org:startDate xmlns:org="urn:example.com/service/org"/>
            </source>
            <target action="child" xpath="//*[local-name()=get-property('RequestType')]"/>
          </enrich>
          <enrich description="populate startDate">
            <source clone="true" property="START_DATE" type="property"/>
            <target
              xmlns:org="urn:example.com/servi`enter code here`ce/org" xpath="//org:startDate"/>
          </enrich>
        </then>
        <else/>
      </filter>
查看更多
我只想做你的唯一
5楼-- · 2019-09-17 21:16

I have achieved a work-around using JavaScript but it should be possible to set it in the property mediator, or may be with enrich.

<script language="js"><![CDATA[var payload = mc.getPayloadJSON(); 
if(payload.MensajeAuditoria.ParentInstanceID== null){
    payload.MensajeAuditoria.ParentInstanceID="";    
}
mc.setPayloadJSON(payload);
mc.setProperty("MyProperty","");]]></script>

If you get an XML solution, please let me know.

Thanks.

查看更多
登录 后发表回答