0

I've created a wso2 data service to retrieve a list of products from a PostgreSQL database:

<data name="ProductListDS" serviceNamespace="" serviceGroup="" transports="http https">
  <description />
  <config id="ProductListDB">
    <property name="driverClassName">org.postgresql.Driver</property>
    <property name="url">jdbc:postgresql://localhost:5432/PRODUCTS</property>
    <property name="username">postgres</property>
    <property name="password">password123</property>
  </config>
  <operation name="QueryProductList">
    <call-query href="QueryProductList" />
  </operation>
  <resource method="GET" path="/products">
    <description />
    <call-query href="QueryProductList" />
  </resource>
  <query id="QueryProductList" useConfig="ProductListDB">
    <sql>SELECT service, name, amount
    FROM products;</sql>
   <result outputType="json">
   {
      "Products": {
         "Product": [
            {
               "service": "$service",
               "name": "$name",
               "amount": "$amount"
            }
         ]
      }
   }
</result>
   
  </query>
</data> 

And I'm calling the data service endpoint inside my API to get JSON data using a call mediator.

<call>
                <endpoint key="getProductListEndpoint"/>
            </call>
            <property expression="$body" name="responsePayload" scope="default" type="STRING"/>
            <log level="full">
                <property expression="get-property('responsePayload')" name="Response Payload"/>
            </log>

But in the logs, I get a xml response:

[2023-09-02 10:15:56,147] INFO {LogMediator} - {api:project_list_api:v1.0.0} To: http://www.w3.org/2005/08/addressing/anonymous, WSAction: , SOAPAction: , MessageID: urn:uuid:542d3384-67e2-4831-bb71-c87bbdc270cc, correlation_id: ff3b8304-2e6a-4718-aca7-a88a8e222853, Direction: request, Response Payload = <soapenv:Body xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"/>, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><Products xmlns="http://ws.wso2.org/dataservice/QueryProductList"><Product><amount>12000</amount><service>Wireless</service><name>1 MONTH</name></Product><Product><amount>24000</amount><service>Wireless</service><name>2 MONTH</name></Product><Product><amount>36000</amount><service>Wireless</service><name>3 MONTH</name></Product><Product><amount>20000</amount><service>Cable</service><name>1 MONTH</name></Product><Product><amount>40000</amount><service>Cable</service><name>2 MONTH</name></Product><Product><amount>60000</amount><service>Cable</service><name>3 MONTH</name></Product></Products></soapenv:Body></soapenv:Envelope>`

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Kavishka Rajapakshe
  • 537
  • 1
  • 8
  • 23

1 Answers1

0

Set the Accept header to application/json before the call mediator.

<property name="Accept" value="application/json" scope="transport"/>
<call>
    <endpoint key="getProductListEndpoint"/>
</call>

Update Also if you don't want to do soap calls remove the following section from your dataservice.

<operation name="QueryProductList">
    <call-query href="QueryProductList" />
</operation>

Then make sure you are doing an HTTP GET to your Dataservice.

<property name="HTTP_METHOD" value="GET" scope="axis2" type="STRING"/>
<property name="Accept" value="application/json" scope="transport"/>
<call>
    <endpoint key="getProductListEndpoint"/>
</call>
ycr
  • 12,828
  • 2
  • 25
  • 45