简体   繁体   English

WSO2 EI JSONBuilder 配置

[英]WSO2 EI JSONBuilder configuration

I have this xml which needed to be processed and converted to JSON:我有这个需要处理并转换为 JSON 的 xml:

<jsonObject>
    <number>1234</number>
    <emptyString/>
    <string>hello</string>
</jsonObject>

When it runs through JsonStreamBuilder like this:当它像这样通过 JsonStreamBuilder 运行时:

<property name="messageType" scope="axis2" type="STRING" value="application/json"/>

I get JSON like that:我得到这样的 JSON:

{
  "number":1234,
  "emptyString": null,
  "string":"hello"
}

So I am wondering is there any way that I can configure JsonStreamBuilder to treat empty tag not as null, but as empty string, furthermore I want numbers to be treated as strings as well.所以我想知道有什么方法可以配置 JsonStreamBuilder 将空标记不视为 null,而是视为空字符串,此外我希望数字也被视为字符串。

Maybe I can modify the default XML to fit my needs?也许我可以修改默认的 XML 以满足我的需要?

BTW I am using WSO2 EI 6.1.1顺便说一句,我正在使用 WSO2 EI 6.1.1

The default behavior of JsonFormatter is to allow primitive types in the JSON output. JsonFormatter 的默认行为是允许 JSON 输出中的原始类型。 This means any content that is a valid number will be represented in the JSON message as a number.这意味着任何有效数字的内容都将在 JSON 消息中表示为数字。

As a solution for this issue, you could disable, the conversion to primitive type after adding below property to the synapse.properties file of EI_HOME/conf directory.作为此问题的解决方案,您可以在将以下属性添加到EI_HOME/conf目录的synapse.properties文件后禁用向原始类型的转换。

synapse.commons.json.output.autoPrimitive=false 

However, this property is a global property, so it will be effected to all the services (Proxy services and the APIs).但是,此属性是一个全局属性,因此它会影响所有服务(代理服务和 API)。 So this means any string that is a valid number will not be converted to a number.所以这意味着任何有效数字的字符串都不会被转换为数字。

According to your issue, you need to disable the conversion of String to Integer.根据您的问题,您需要禁用 String 到 Integer 的转换。 You could solve this, adding below properties into EI_HOME/conf/synapse.properties file also.您可以解决此问题,同时将以下属性添加到EI_HOME/conf/synapse.properties文件中。

synapse.commons.json.output.autoPrimitive=true

synapse.commons.json.output.disableAutoPrimitive.regex=^[1-9]*$

In this case, auto primitive disables for the numbers only.在这种情况下,auto primitive 仅对数字禁用。

NOTE : Once autoprimitive is enabled, some fields can be excluded from the auto primitive functionality based on the regular expression defined in the synapse.properties file.注意:启用自动原语后,可以根据 synapse.properties 文件中定义的正则表达式将某些字段从自动原语功能中排除。

  • And the empty tag as the not null issue, you can resolve following the above-mentioned blog post.而空标签作为not null的问题,可以按照上述博文解决。

This looks like restriction in wso itlsef, documentation states that is desired behavior.这看起来像是 wso itlsef 中的限制,文档指出这是所需的行为。 To overcome issue have try script mediator WSO2ESB: Property setting not accepting empty value or getting even lower (more complicated) https://medium.com/@lashan/class-mediator-to-handle-null-values-in-json-payload-wso2-ei-34a43246f0f要克服问题,请尝试脚本中介WSO2ESB:属性设置不接受空值或变得更低(更复杂) https://medium.com/@lashan/class-mediator-to-handle-null-values-in-json-有效负载-wso2-ei-34a43246f0f

If you were using a more recent WSO version, there is a solution: https://docs.wso2.com/display/EI630/Working+with+Message+Payloads#WorkingwithMessagePayloads-EmptyXMLelementswiththe'nil'attribute如果您使用的是更新的 WSO 版本,则有一个解决方案: https ://docs.wso2.com/display/EI630/Working+with+Message+Payloads#WorkingwithMessagePayloads-EmptyXMLelementswiththe'nil'attribute

You can set synapse configs locally with the help of JSON Transform mediator您可以在JSON 转换中介的帮助下在本地设置突触配置

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

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