将基于 JSON 的 REST - ESB - REST 通信构建 AXIS 2 XML MessageContext [英] Will JSON based REST - ESB - REST communication build AXIS 2 XML MessageContext

查看:23
本文介绍了将基于 JSON 的 REST - ESB - REST 通信构建 AXIS 2 XML MessageContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于 JSON 的端到端 REST 服务的用例,即 REST 客户端 -> ESB -> REST 服务(全部在 JSON 上),我计划使用 API 和;用于转换的 PayLoad 工厂

I have a use-case for end to end JSON based REST services i.e. REST Client -> ESB -> Rest Service (all on JSONs) which I am planning to handle using APIs & PayLoad factory for transformations

  1. 在这种情况下,WSO2 ESB 是否会在调用目标 Rest Service 之前将 JSON 转换为 XML?

  1. In this scenario, will WSO2 ESB convert JSON into XML before invoking the target Rest Service?

如果 #1 没有,那么它将如何准备 MessageContext 对象,该对象看起来具有所有 XML 构造,如 SOAPEnvevelope、Body 等?

If no for #1, then how will it prepare MessageContext object which looks having all XML constrcuts like SOAPEnvevelope, Body etc.?

更新 1 :为了进一步澄清我的问题,我已经更新了我的示例 API &自定义中介

Update 1 : To further clarify my question, I have updated this my sample API & custom 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>

自定义中介类

public class MyCustomerMediator extends AbstractMediator { 

    public boolean mediate(MessageContext context) { 
        System.out.println("In My Custom Mediator 44 $$" + context.getEnvelope().getBody() +"$$");
        return true;
    }
}

控制台输出

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 时得到了正确的响应,但是打印的消息上下文正文没有任何内容.那么 MessageContext 中存储的 JSON 消息在哪里?

I am getting proper response when invoking the API, however the printed Body of message context is not having any content. So where is the JSON message being stored in MessageContext?

更新 2我在axis2.xml 中的构建器和格式化程序配置实际上是WSO2 ESB 4.9.0 SNAPSHOT 中的默认配置

Update 2 My builder and formatter configuration in axis2.xml which were actually default in 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 请求中的内容类型.

The complete configuration file is available @ http://www.pastebin.ca/3038336 I have set application/json as the content type in the request to ESB API.

谢谢,苦涩

推荐答案

对于您的方案,您只需首先检查axis2.xml 配置中是否包含适当的JSON 消息生成器和格式化程序.在这种情况下,原始消息将被正确解析,目标服务将使用 JSON 调用.

For your scenario you only need to check first that appropriate JSON message builder and formatter are included in your axis2.xml config. In this case the original message will be parsed correctly and target service will be called with JSON.

尚未真正将这种格式用于 ESB,但您的有效负载应该像往常一样出现在 MessageContext 的 SOAP Envelope 正文中.

Have not really used this format with ESB, but your payload should appear in body of the SOAP Envelope in MessageContext, as usual.

更新

使用给定的构建器和格式化程序,信封的空体看起来是预期的结果.正如我从代码中看到的,JSONStreamBuilder 只创建空信封并在消息上下文中保存对输入流的引用:

With given builder and formatter empty body of envelope looks to be expected result. As I see from the code, JSONStreamBuilder only creates empty envelope and saves reference to input stream in message context:

messageContext.setProperty("JSON_STREAM", inputStream);

所以你可以用你的自定义中介来捕捉它:

So you can catch it with your custom mediator as:

messageContext.getProperty("JSON_STREAM");

但如果您想找到转换为 XML 的 JSON,则需要改用 JSONBuilder.

But if you'd like to find JSON converted to XML, you need to use JSONBuilder instead.

这篇关于将基于 JSON 的 REST - ESB - REST 通信构建 AXIS 2 XML MessageContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆