I want to send a query to the database and return the result to the client using WSO2 rest api. Here is the synapse config:
<api xmlns="http://ws.apache.org/ns/synapse" name="RestDBLookup" context="/dblookup">
<resource methods="POST GET" uri-template="/channel/{name}" protocol="http">
<inSequence>
<dblookup>
<connection>
<pool>
<password>pass</password>
<driver>oracle.jdbc.driver.OracleDriver</driver>
<url>jdbc:oracle:thin:@localhost:1521:ORCL</url>
<user>user</user>
</pool>
</connection>
<statement>
<sql>SELECT ID, CHANNEL_NAME FROM CHANNEL where CHANNEL_NAME = ?</sql>
<parameter expression="get-property('uri.var.name')" type="VARCHAR"/>
<result name="channel_id" column="ID"/>
</statement>
</dblookup>
<log level="custom">
<property name="ID" expression="get-property('channel_id')"/>
<property name="State" value="after db"/>
</log>
</inSequence>
<outSequence>
<property name="messageType" value="application/json" scope="axis2" type="STRING"/>
<log level="full"/>
<send/>
</outSequence>
<faultSequence/>
</resource>
</api>
When I call the url http://localhost:8280/dblookup/channel/someChannel
with rest client I get the log after the dblookup
LogMediator ID = 40810162, State = after db
but the outSequence
is not executed and I only get Status 202 Accepted on the rest client with no body.
So the question is how can I construct some response (in JSON format for example) and send it to the client?
Synapse mediation has three flows:
Now let's see your use-case. What you trying to do is query database in inSequence. Also you getting result as well. But problem is result not send back to client. Here problem was the mediation you written in inSequence not calling the outSequence. Therefore outSequence not getting hit. For an example if you call backend service in inSequence, then response hit to outSequence. In your case response come back to inSequence. Therefore we can do either one of following option:
<loopback/>
mediator after </log>
mediator in inSequence.
This will move message from inSequence to outSequence.<respond/>
at the end of inSequence. This will send current
message back to client. So you may not need to have outSequence.Hope above answer helps.
Cheers!