user3129056
user3129056

Reputation: 447

Json output required at client side

I'm using wso2 4.7.0 and wso2dss 3.0.0..I have created a service in which my client sending clientcode in request and i have to responce with clientname and clientid.I have created a proxy serivce and sequence for it like :

<inSequence onError="fault">
         <property name="messageType" value="application/json" scope="axis2"/>
         <property name="clientcode"
                   expression="//clientcode/text()"
                   scope="default"
                   type="STRING"/>
         <payloadFactory>
            <format>
               <p:Select_Clientid_Op xmlns:p="http://ws.wso2.org/dataservice">
                  <xs:clientcode xmlns:xs="http://ws.wso2.org/dataservice">$1</xs:clientcode>
               </p:Select_Clientid_Op>
            </format>
            <args>
               <arg evaluator="xml" expression="//clientcode/text()"/>
            </args>
         </payloadFactory>
         <send receive="Client_Seq2.0">
            <endpoint>
               <address uri="http://localhost:9764/services/muser_DataService2.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>

Sequence:

<sequence xmlns="http://ws.apache.org/ns/synapse" name="Client_Seq3.0">
   <property name="messageType" value="application/json" scope="axis2"/>
   <property name="FORCE_ERROR_ON_SOAP_FAULT" value="true"/>
   <property xmlns:ns="http://org.apache.synapse/xsd" xmlns:s="http://ws.wso2.org/dataservice" name="clientid" expression="//s:clientid/text()" scope="default" type="STRING"/>
   <property xmlns:ns="http://org.apache.synapse/xsd" xmlns:s="http://ws.wso2.org/dataservice" name="clientname" expression="//s:clientname/text()" scope="default" type="STRING"/>
   <property xmlns:ns="http://org.apache.synapse/xsd" xmlns:s="http://ws.wso2.org/dataservice" name="clientshortname" expression="//s:clientshortname/text()" scope="default" type="STRING"/>
   <log>
      <property xmlns:ns="http://org.apache.synapse/xsd" name="clientid" expression="get-property('clientid')"/>
   </log>
   <filter xmlns:ns="http://org.apache.synapse/xsd" xpath="get-property('clientid')=''">
      <then>
         <payloadFactory>
            <format>
               <ResponseJSON xmlns="">
                  <Exception> Data Not Found</Exception>
                  <Status>404</Status>
               </ResponseJSON>
            </format>
         </payloadFactory>
         <send/>
      </then>
      <else>
         <payloadFactory>
            <format>
               <ResponseJSON xmlns="">
                  <Body>
                     <Datalist>
                        <clientid>$1</clientid>
                        <clientname>$2</clientname>
                        <clientshortname>$2</clientshortname>
                     </Datalist>
                  </Body>
                  <Status>200</Status>
               </ResponseJSON>
            </format>
            <args>
               <arg xmlns:s="http://ws.wso2.org/dataservice" expression="//s:clientid/text()" evaluator="xml"/>
               <arg xmlns:s="http://ws.wso2.org/dataservice" expression="//s:clientname/text()" evaluator="xml"/>
               <arg xmlns:s="http://ws.wso2.org/dataservice" expression="//s:clientshortname/text()" evaluator="xml"/>
            </args>
         </payloadFactory>
         <log level="full"/>
         <send/>
      </else>
   </filter>
</sequence>

curl :

 curl -v -H "Accept:application/json" -H "Content-Type:application/json"  -d '{"clientcode":"youtility"}' http://youtility-desktop:8282/services/PartyClientid3.0

I got xml format output at client side as :<ResponseJSON><Body><Datalist><clientid>21405735715865601</clientid><clientname>Youtility tech</clientname><clientshortname>Youtility</clientshortname></Datalist></Body><Status>200</Status></ResponseJSON>

and i required JSON format as:

{
  "ResponseJSON": {
    "Body": {
      "Datalist": {
        "clientid": "21405735715865601",
        "clientname": "Youtility tech",
        "clientshortname": "Youtility"
      }
    },
    "Status": "200"
  }
}

where should i change my code?or i have to change my payloadfactory?if yes then how?

Upvotes: 1

Views: 210

Answers (3)

mihir S
mihir S

Reputation: 647

Remove -H "Content-Type:application/json" from your curl command and try once

Upvotes: 0

Dakshika
Dakshika

Reputation: 1714

since your expecting JSON format you need to update playLoadFactory correctly.

<payloadFactory media-type="json">
 <format>{ "clientid": "$1", "clientname": "$2", "clientshortname": "$3"}</format>
 <args>
 <arg expression="$.clientid" evaluator="json"></arg>
 <arg expression="$.clientname" evaluator="json"></arg>
 <arg expression="$.clientshortname" evaluator="json"></arg>
 </args>
 </payloadFactory>
<property name="messageType" value="application/json" scope="axis2"/>

try this

http://technotes.dilan.me/?p=246

Upvotes: 0

mephist
mephist

Reputation: 383

You should set "messageType" property before Send mediator.

<property name="messageType" value="application/json" scope="axis2"/>
<send/>

Upvotes: 1

Related Questions