我有一个基于JSON的端到端REST服务的用例,即REST客户端 - > ESB - > Rest服务(全部在JSON上),我计划使用API和PayLoad工厂进行转换
-
在这种情况下,WSO2 ESB会在调用目标Rest服务之前将JSON转换为XML吗?
-
如果没有#1,那么它将如何准备MessageContext对象,它看起来像SOAPEnvevelope,Body等所有XML构造?
Update 1 : 为了进一步澄清我的问题,我更新了我的示例API和自定义Mediator
api/TestRestAPI.xml
<api xmlns="http://ws.apache.org/ns/synapse"
name="TestRestAPI"
context="/testrest">
<resource methods="POST GET">
<inSequence>
<class name="com.example.wso2.esb.mediators.custom.MyCustomerMediator"/>
<send>
<endpoint>
<address uri="http://jsonplaceholder.typicode.com/posts"/>
</endpoint>
</send>
</inSequence>
<outSequence>
<class name="com.example.wso2.esb.mediators.custom.MyCustomerMediator"/>
<send/>
</outSequence>
</resource>
</api>
Custom Mediator Class
public class MyCustomerMediator extends AbstractMediator {
public boolean mediate(MessageContext context) {
System.out.println("In My Custom Mediator 44 $$" + context.getEnvelope().getBody() +"$$");
return true;
}
}
Console Output
In My Custom Mediator 44 $$<soapenv:Body
xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"/>$$
In My Custom Mediator 44 $$<soapenv:Body
xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"/>$$
我在调用API时得到了正确的响应,但是打印的消息上下文没有任何内容 . 那么JSON消息存储在MessageContext中的哪个位置?
Update 2 我在axis2.xml中的构建器和格式化程序配置实际上是WSO2中的默认值ESB 4.9.0 SNAPSHOT
<!--JSON Message Formatters-->
<messageFormatter contentType="application/json"
class="org.apache.synapse.commons.json.JsonStreamFormatter"/>
<!--JSON Message Builders-->
<messageBuilder contentType="application/json"
class="org.apache.synapse.commons.json.JsonStreamBuilder"/>
完整的配置文件可用@ http://www.pastebin.ca/3038336我已将 application/json 设置为ESB API请求中的内容类型 .
谢谢,哈里什
2 回答
在Rest API的内部,您将自动获得XML格式的内容 . 当使用rest数据访问api时,它将自动转换为XML .
然后,您可以使用有效负载工厂介体来更改内容 . 然后响应将自动再次转换为json .
这就是你需要的 . 所以没有必要从你身边“转换” . 请创建一个特定的REST API,并在有效问题的情况下将其发布在此处 .
对于您的方案,您只需先检查您的axis2.xml配置中是否包含适当的JSON消息构建器和格式化程序 . 在这种情况下,将正确解析原始消息,并使用JSON调用目标服务 .
没有真正使用这种格式与ESB,但您的有效负载应该像往常一样出现在MessageContext中SOAP信封的正文中 .
UPDATE
随着给定的构建器和格式化器信封的空体看起来是预期的结果 . 正如我从代码中看到的那样,JSONStreamBuilder只创建空信封并在消息上下文中保存对输入流的引用:
因此,您可以使用自定义调解器捕获它:
但是,如果您希望将JSON转换为XML,则需要使用JSONBuilder .