I want use WSO2 ESB as a gateway. I'm using version 4.0.3 I have some existing RESTful services with JSON message. I know ESB now has REST API supported. But I still can't find solution for WSO2 ESB REST to REST. I mean all backend services are RESTful with JSON format. Can anyone help me?
4 Answers
Yes we do support REST -REST services, which means its categorize under protocol switching, with WSO2 ESB it has REST API which which enables you to handle incoming REST or any other format to and do mediation and pass them to the back end (its doesn't matter which protocol is)
following proxy allows you to transfer incomming REST message to back end REST service basically if you need to access incomming form data you may have to use
<messageFormatter contentType="application/x-www-form-urlencoded" class="org.apache.axis2.transport.http.XFormURLEncodedFormatter"/>
<messageBuilder contentType="application/x-www-form-urlencoded" class="org.apache.synapse.commons.builders.XFormURLEncodedBuilder"/>
which allows you to extract incomming REST submit details and do any mediation as you prefer
REST TO REST VIA REST API
<api name="studentSecureAPI" context="/SecureStudentRequest">
<resource methods="POST" uri-template="/student/{name}">
<inSequence>
<property name="REST_URI" expression="fn:substring($axis2:REST_URL_POSTFIX,16,fn:string-length($axis2:REST_URL_POSTFIX))"/>
<property name="AGE" expression="//xformValues//age"/>
<property name="STUDENT" expression="get-property('uri.var.name')"/>
<property name="DISABLE_CHUNKING" value="true" scope="axis2" type="STRING"/>
<property name="HTTP_METHOD" value="POST" scope="axis2" type="STRING"/>
<property name="ContentType" value="application/x-www-form-urlencoded" scope="axis2" type="STRING"/>
<property name="REST_URL_POSTFIX" scope="axis2" action="remove"/>
<property name="REST_URL_POSTFIX" expression="$ctx:REST_URI" scope="axis2"/>
<payloadFactory>
<format>
<POST>
<age>$1</age>
</POST>
</format>
<args>
<arg expression="$ctx:AGE"/>
</args>
</payloadFactory>
<send>
<endpoint>
<address uri="http://localhost:9764/as/services/RestService"/>
</endpoint>
</send>
</inSequence>
<outSequence>
<send/>
</outSequence>
</resource>
REST TO REST VIA SIMPLE PROXY :
<proxy name="StudentRequestProxy" transports="https http" startOnLoad="true" trace="disable">
<target>
<inSequence>
<property name="DISABLE_CHUNKING" value="true" scope="axis2" type="STRING"/>
<property name="ContentType" value="text/xml" scope="axis2" type="STRING"/>
<property xmlns:ns3="http://org.apache.synapse/xsd" name="Lang" expression="get-property('transport', 'Accept')" scope="default" type="STRING"/>
<log level="custom">
<property name="HTTP_METHOD IS###########" expression="$axis2:HTTP_METHOD"/>
</log>
<switch source="$axis2:HTTP_METHOD">
<case regex="GET">
<property name="HTTP_METHOD" value="GET" scope="axis2" type="STRING"/>
</case>
<case regex="POST">
<property name="HTTP_METHOD" value="POST" scope="axis2" type="STRING"/>
</case>
<default/>
</switch>
<send>
<endpoint>
<address uri="http://localhost:9764/as/services/RestService"/>
</endpoint>
</send>
</inSequence>
<outSequence>
<send/>
</outSequence>
</target>
</proxy>
Latest ESB version (4.8.1) is perfectly working with JSON REST calls. try following payloadFactory and property mediators,
<payloadFactory media-type="json">
<format>
{
"name":"$1",
"age":$2
}
</format>
<args>
<arg evaluator="json" expression="$ctx:name"/>
<arg evaluator="json" expression="$ctx:age"/>
</args>
</payloadFactory>
<property name="messageType" value="application/json" scope="axis2"/>
More information can be found at here