## Apache Synapse

## Sample 800: Introduction to REST APIs

<definitions xmlns="http://ws.apache.org/ns/synapse"> <api name="StockQuoteAPI" context="/stockquote"> <resource uri-template="/view/{symbol}" methods="GET"> <inSequence> <payloadFactory> <format> <m0:getQuote xmlns:m0="http://services.samples"> <m0:request> <m0:symbol>$1</m0:symbol> </m0:request> </m0:getQuote> </format> <args> <arg expression="get-property('uri.var.symbol')"/> </args> </payloadFactory> <send> <endpoint> <address uri="http://localhost:9000/services/SimpleStockQuoteService" format="soap11"/> </endpoint> </send> </inSequence> <outSequence> <send/> </outSequence> </resource> <resource url-pattern="/order/*" methods="POST"> <inSequence> <property name="FORCE_SC_ACCEPTED" value="true" scope="axis2"/> <property name="OUT_ONLY" value="true"/> <send> <endpoint> <address uri="http://localhost:9000/services/SimpleStockQuoteService" format="soap11"/> </endpoint> </send> </inSequence> </resource> </api> </definitions>

### Objective

APIs in Synapse provide a convenient approach for receiving and processing REST traffic through the service bus. APIs can be used to receive specific types of RESTful invocations and then process them through a set of user defined resources. This sample is aimed at introducing the basic capabilities of APIs and how they are configured to front existing services.

### Pre-requisites

- Deploy the SimpleStockQuoteService in the sample Axis2 server and start Axis2 Server
- Start Synapse using the configuration numbered 800 (repository/conf/sample/synapse_sample_800.xml)
  Unix/Linux: sh synapse.sh -sample 800
  Windows: synapse.bat -sample 800

### Executing the REST Client

You might need a REST client like curl to test this

curl -v http://127.0.0.1:8280/stockquote/view/IBM
curl -v http://127.0.0.1:8280/stockquote/view/MSFT

The above GET calls will be handled by the first resource in the StockQuoteAPI. These REST calls will get converted into SOAP calls and will be sent to the Axis2 server. Response will be sent to the client in POX format.

The following command POSTs a simple XML to the ESB. Save following sample place order request as "placeorder.xml" file in your local file system and execute the command. That is used to invoke a SOAP service. ESB returns the 202 response back to the client.

curl -v -d @placeorder.xml -H "Content-type: application/xml" http://127.0.0.1:8280/stockquote/order/
<placeOrder xmlns="http://services.samples"> <order> <price>50</price> <quantity>10</quantity> <symbol>IBM</symbol> </order> </placeOrder>

Back to Catalog