我的后端服务响应 XML,我默认返回它. 如果客户端给我一个adds参数(比如: outout_format = json),我需要在“outSequence”中将响应转换为JSON. 例如: resultfoobar/foofoo2bar2/foo2nestednodevalue/node/neste
如果客户端给我一个adds参数(比如:& outout_format = json),我需要在“outSequence”中将响应转换为JSON.
例如:
<result> <foo>bar</foo> <foo2>bar2</foo2> <nested> <node>value</node> </nested> </result>
应该作为回应
{
"foo": "bar",
"foo2": "bar2",
"nested":{
"node":"value"
}
}
这是一个测试代理服务(我只是在这里使用inSequence来显示问题):
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="JSONtest"
transports="https,http"
statistics="disable"
trace="disable"
startOnLoad="true">
<target>
<inSequence>
<property name="TEST_WAITING_FOR"
value="json"
scope="default"
type="STRING"/>
<header name="To" action="remove"/>
<property name="RESPONSE" value="true"/>
<property name="NO_ENTITY_BODY" scope="axis2" action="remove"/>
<payloadFactory media-type="xml">
<format>
<response xmlns="">
<result>success</result>
<code>123</code>
</response>
</format>
<args/>
</payloadFactory>
<switch source="get-property('TEST_WAITING_FOR')">
<case regex="json">
<property name="messageType"
value="application/json"
scope="axis2"
type="STRING"/>
</case>
<default>
<property name="messageType" value="text/xml" scope="axis2" type="STRING"/>
</default>
</switch>
<send/>
</inSequence>
</target>
<description/>
它回应:
{"response":{"result":"success","code":123}}
有没有办法删除根节点“响应”,使其看起来像这样?
{"result":"success","code":123}
当我尝试使用Enrich-mediator(例如$body / result / * – > $body / *)删除节点“result”时,它变为带有多个根节点的无效XML,而JSON只包含第一个节点.
我知道有可能Payload一个JSON消息,但后端可以返回一个具有不同格式和不同数量的嵌套节点的XML,所以我不能将它硬编码为JSON.
看来我需要实现自己的JSONMessageFromatter? (任何代码示例?)
UPD:我找到了解决方案(感谢Dakshika)
<payloadFactory media-type="json">
<format>$1</format>
<args>
<arg expression="$.response" evaluator="json"></arg>
</args>
</payloadFactory>
在ESB 4.7版本中,有效负载工厂介体支持多种介质类型,xml和json,您可以相应地进行更改.
请尝试以下链接
http://www.dilan.me/articles/tech-notes/how-to-use-wso2-products-esb-payload-factory-mediator-to-change-json-service-output/
http://wso2.com/library/articles/2013/12/restful-integration-with-wso2-esb/
