如何在wso2esb 4.9.0中避免JSON响应中的jsonObject

时间:2015-09-28 09:06:00

标签: json wso2 axis2 wso2esb wso2carbon

使用Wso2esb 4.9.0, 我有一个服务,它从数据库中选择数据并以JSON格式给出响应。

代理服务:

<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="GetMquestionMobile3.0"
       transports="https http"
       startOnLoad="true"
       trace="disable"
       statistics="enable">
   <description/>
   <target>
      <inSequence onError="fault">
         <property name="messageType" value="application/json" scope="axis2"/>
         <property name="FORCE_ERROR_ON_SOAP_FAULT" value="true"/>
         <property name="userid" expression="get-property('transport', 'userid')"/>
         <property name="ModifiedOn" expression="get-property('transport', 'ModifiedOn')"/>
         <property name="username" expression="get-property('transport', 'username')"/>
         <property name="password" expression="get-property('transport', 'password')"/>
         <property name="PartyBranchID"
                   expression="//FieldValue/text()"
                   scope="default"
                   type="STRING"/>
         <property name="usercode"
                   expression="fn:substring-before(get-property('username'),'|')"
                   scope="default"
                   type="STRING"/>
         <property name="clientid"
                   expression="fn:substring-after(get-property('username'),'|')"
                   scope="default"
                   type="STRING"/>
<payloadFactory media-type="xml">
            <format>
               <p:Getmquestions_Mobile xmlns:p="http://ws.wso2.org/dataservice">
                  <p:clientid>$1</p:clientid>
                  <p:modifiedon>$2</p:modifiedon>
               </p:Getmquestions_Mobile>
            </format>
            <args>
               <arg evaluator="xml" expression="get-property('clientid')"/>
               <arg evaluator="xml" expression="get-property('ModifiedOn')"/>
            </args>
         </payloadFactory>
         <send receive="MquestionMobile_Seq33.0">
            <endpoint>
               <address uri="http://localhost:9764/services/mquestions_DataService3.0/"
                        format="soap11">
                  <suspendOnFailure>
                     <errorCodes>101500,101501,101506,101507,101508,101503,50000</errorCodes>
                     <initialDuration>30</initialDuration>
                     <progressionFactor>1.0</progressionFactor>
                     <maximumDuration>300</maximumDuration>
                  </suspendOnFailure>
               </address>
            </endpoint>
         </send>
</inSequence>
      <outSequence onError="fault">
         <send/>
      </outSequence>
   </target>
</proxy>

序列:

<sequence xmlns="http://ws.apache.org/ns/synapse"
          name="MquestionMobile_Seq33.0"
          statistics="enable">
   <property name="messageType" value="application/json" scope="axis2"/>
   <property name="FORCE_ERROR_ON_SOAP_FAULT" value="true"/>
   <property name="HTTP_METHOD" value="POST" scope="axis2"/>
   <property xmlns:ns="http://org.apache.synapse/xsd"
             xmlns:s="http://ws.wso2.org/dataservice"
             name="Datalist"
             expression="//s:Datalist"
             scope="default"
             type="STRING"/>
   <property xmlns:ns="http://org.apache.synapse/xsd"
             xmlns:s="http://ws.wso2.org/dataservice"
             name="Total"
             expression="count(//s:Datalist)"
             scope="default"
             type="STRING"/>
<filter xmlns:ns="http://org.apache.synapse/xsd"
           xpath="get-property('Total')='0.0'">
      <then>
         <property name="RESPONSE" value="true" scope="default" type="STRING"/>
         <payloadFactory media-type="json">
            <format>
        {"ResponseJSON":{"Body":{"Datalist":"Not Found"},"Status":"404"}}
    </format>
            <args/>
         </payloadFactory>
         <send/>
      </then>
      <else>
         <property name="RESPONSE" value="true" scope="default" type="STRING"/>
         <property name="NO_ENTITY_BODY" scope="axis2" action="remove"/>
         <payloadFactory media-type="json">
            <format>{"ResponseJSON" : {"Body" :$1,"Status" :"200","Total" :"$2"}}</format>
            <args>
                <arg evaluator="json" expression="$.Body"/>
                <arg evaluator="xml" expression="get-property('Total')"/>
            </args>
         </payloadFactory>
         <send/>
      </else>
   </filter>   
</sequence>

此服务为我提供了如下所示的响应

{"jsonObject":{"ResponseJSON" : 
    {"Body" :
    {"Datalist":[
    {"ComboValues":"Yes,No","QuestionType":0,"Tag":-1,"Question":"LogBooks","IsAVPT":-1,"QuestionId":989181535979317,"DataSize":-1.0,"ToolTip":-1,"IsFollowup":-1},
    {"ComboValues":"Yes,No","QuestionType":0,"Tag":null,"Question":"MasterRegister","IsAVPT":-1,"QuestionId":989181536700214,"DataSize":-1.0,"ToolTip":null,"IsFollowup":-1},
    {"ComboValues":null,"QuestionType":0,"Tag":null,"Question":"OthersRecords","IsAVPT":-1,"QuestionId":989181536732983,"DataSize":-1.0,"ToolTip":null,"IsFollowup":-1}]},"Status":"200","Total":"3.0"}}}

我不想在我的回复中看到jsonObject,我如何在我的JSON响应中避免这种情况并获得如下响应

{"ResponseJSON" : 
        {"Body" :
        {"Datalist":[
        {"ComboValues":"Yes,No","QuestionType":0,"Tag":-1,"Question":"LogBooks","IsAVPT":-1,"QuestionId":989181535979317,"DataSize":-1.0,"ToolTip":-1,"IsFollowup":-1},
        {"ComboValues":"Yes,No","QuestionType":0,"Tag":null,"Question":"MasterRegister","IsAVPT":-1,"QuestionId":989181536700214,"DataSize":-1.0,"ToolTip":null,"IsFollowup":-1},
        {"ComboValues":null,"QuestionType":0,"Tag":null,"Question":"OthersRecords","IsAVPT":-1,"QuestionId":989181536732983,"DataSize":-1.0,"ToolTip":null,"IsFollowup":-1}]},"Status":"200","Total":"3.0"}}

DS回复:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header/>
   <soapenv:Body>
      <Body xmlns="http://ws.wso2.org/dataservice">
         <Datalist>
            <Question>LogBooks</Question>
            <QuestionId>989181535979317</QuestionId>
            <QuestionType>0</QuestionType>
            <ComboValues>Yes,No</ComboValues>
            <IsFollowup>-1</IsFollowup>
            <IsAVPT>-1</IsAVPT>
            <DataSize>-1.0</DataSize>
            <ToolTip>-1</ToolTip>
            <Tag>-1</Tag>
            <weightage>1.00</weightage>
         </Datalist>
         <Datalist>
            <Question>Master Register</Question>
            <QuestionId>989181536700214</QuestionId>
            <QuestionType>0</QuestionType>
            <ComboValues>Yes,No</ComboValues>
            <IsFollowup>-1</IsFollowup>
            <IsAVPT>-1</IsAVPT>
            <DataSize>-1.0</DataSize>
            <ToolTip/>
            <Tag/>
            <weightage>1.00</weightage>
         </Datalist>
         <Datalist>
            <Question>Others Records</Question>
            <QuestionId>989181536732983</QuestionId>
            <QuestionType>0</QuestionType>
            <ComboValues/>
            <IsFollowup>-1</IsFollowup>
            <IsAVPT>-1</IsAVPT>
            <DataSize>-1.0</DataSize>
            <ToolTip/>
            <Tag/>
            <weightage>1.00</weightage>
         </Datalist>
          </Body>
   </soapenv:Body>
</soapenv:Envelope>

2 个答案:

答案 0 :(得分:1)

您可以使用rich mediator从响应主体中删除jsonObject,如下所示。

<iframe width="560" height="315" src="video.mp4"></iframe>

您可以在丰富介体后添加日志介体并检查更改的主体。

希望这会对你有所帮助。

答案 1 :(得分:0)

在侧面ESB中介通信以SOAP消息的形式发生。当您发送JSON请求或ESB收到JSON响应时,ESB通过添加标记将其转换为xml / soap消息以防止多根xml。 尝试使用

{% extends "base.html" %}
{% block content %}

<body>
<section>
    ...
</section>
<iframe src="{% for item in query_set %}{{ item }}{% endfor %}" style="width:100%;height:100%;position:absolute;margin-top:-10px;"></iframe>
{% endblock %}

发送/致电之前。这将自动从响应中删除您的jsonObject。但是如果你想从json将响应转换为xml,那么你需要使用enrich mediator

手动修改

确保在axis2.xml中启用了json格式化程序