简体   繁体   English

wso2在序列内部将XML转换为Json

[英]wso2 Transform XML to Json inside sequence

I receive from an rabbitmq inbound an XML message and I have to send it to mongodb with restheart. 我收到来自Rabbitmq的入站XML消息,我必须使用restheart将其发送到mongodb。 I must modify the content of the Json generate by the formatter before sending it to the mongo rest api, because I must modify a datetime format send in string to add "ISOdate ()" for mongodb. 我必须先修改格式化程序生成的Json的内容,然后再将其发送到mongo rest api,因为我必须修改日期时间格式的字符串发送方式,以便为mongodb添加“ ISOdate()”。

My incomming message can be different from other messages. 我的传入消息可能与其他消息不同。

For exemple, I have this from inbound : 例如,我从入站得到这个:

<CONTENT><CODE_USER>100</CODE_USER><DATE>2017-12-12</DATE></CONTENT>

and I get this for the endpoint, 我得到了这个端点

{"CONTENT" : {"CODE_USER":100","DATE":"2017-12-12"}} {“ CONTENT”:{“ CODE_USER”:100“,” DATE“:” 2017-12-12“}}

So I want to modify the Json format before sending to the endpoint to modify the date string. 所以我想在发送到端点以修改日期字符串之前修改Json格式。

Is there a way to trigger the esb Json formatter and get the result before send it to the endpoint ? 有没有一种方法可以触发esb Json格式化程序并获取结果,然后再将其发送到端点?

Thanks, Nicolas 谢谢,尼古拉斯

I find an solution, 我找到了解决方案,

I use json-eval($.*.) function and an script mediator to modify the date format before sending to the endpoint, like this : 我使用json-eval($。*。)函数和脚本介体在发送到端点之前修改日期格式,如下所示:

 <property expression="json-eval($.*.)" name="location" scope="default" type="STRING"/>

<script language="js"><![CDATA[var message =  new String(mc.getProperty('location'));
  var reg = /\"((\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)\.(\d{1,3})Z)\"/g;
  var mess=message.replace (reg,'{\"$date\" : \"\$1\"}');
  mess=mess.substring (1,mess.length-1);
  mc.setPayloadJSON(mess);]]></script>

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

Yes that is possible. 是的,那是可能的。 You can add a mediator to the in-sequence such that the transformation happens before releasing the response to mongo-db. 您可以将中介程序添加到顺序中,以便在将响应发布到mongo-db之前进行转换。

Please check the available mediators and if there is no any suitable one for your requirement, you can simply write your own mediator by extending org.apache.synapse.mediators.AbstractMediator 请检查可用的介体,如果没有适合您的介体,则可以通过扩展org.apache.synapse.mediators.AbstractMediator来编写自己的介体。

For the details on how to write a custom mediator, you can refer the blog: https://medium.com/@nirothipanram/esb-few-tips-in-writing-a-custom-class-mediator-b9a322f4eaa8 有关如何编写自定义调解器的详细信息,请参阅以下博客: https : //medium.com/@nirothipanram/esb-few-tips-in-writing-a-custom-class-mediator-b9a322f4eaa8

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

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