Will JSON based REST - ESB - REST communication build AXIS 2 XML MessageContext

隐身守侯 提交于 2019-12-12 04:24:46

问题


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. In this scenario, will WSO2 ESB convert JSON into XML before invoking the target Rest Service?

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

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>

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"/>$$

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?

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"/>

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.

Thanks, Harish


回答1:


Internally of the Rest API you will automatically have the content in XML. When accessing the api with rest data, it will automatically transformed to XML.

Then you can use the payload factory mediator to change the content. Then the response will automatically again transformed to json.

And that is what you need. So no "conversion" from your side necessary. Please create a specific REST API and post it here in case of an effective issue.




回答2:


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.

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

UPDATE

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");

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



来源:https://stackoverflow.com/questions/31055149/will-json-based-rest-esb-rest-communication-build-axis-2-xml-messagecontext

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!