Thursday, November 17, 2011

Invoking a RESTful service via WSO2ESB

In the ESB, when we define a proxy, it is not a must to provide the service wsdl, unless if you want to hide/add a new elements in your service contract.
Lets check a  very simple basic proxy to process a HTTP GET request
  • Get wso2esb binary distribution.
  • Deploy the simplestockquote service and start the sample axis2server.
  • Start ESB.
Create the following proxy;
<proxy name="RestProxy" transports="https http" startOnLoad="true" trace="disable">
        <target>  
            <endpoint>
              <address uri="http://localhost:9000/services/SimpleStockQuoteService"/>
            </endpoint>             
            <outSequence>
                <send/>
            </outSequence>
        </target>
    </proxy>

Execute the following GETrequest url in your browser;
http://localhost:8280/services/RestProxy/getSimpleQuote?symbol=IBM

Now you will see the response in your browser. If you place the tcpmon between client and ESB, you can check your http 'get' request,

GET /services/RestProxy/getSimpleQuote?symbol=IBM HTTP/1.1
Host: 127.0.0.1:5555
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.24) Gecko/20111103 Firefox/3.6.24
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Cookie: region1_configure_menu=; region3_registry_menu=visible; region4_monitor_menu=;

In the above sample client sends the 'symbol=IBM' as the parameter for the operation "getSimpleQuote"..Say in the mediation logic user may need to change the parameter as 'SUN'.(ie:symbol=SUN)..For that he/she can use "REST_URL_POSTFIX" property ..Which basically adds the suffix for the REST service endpoint.
eg:
   <property name="REST_URL_POSTFIX" value="/getSimpleQuote?symbol=SUN" scope="axis2"/>

So, the insequence would be;
    <inSequence>
                <property name="REST_URL_POSTFIX" value="/getSimpleQuote?symbol=SUN" scope="axis2"/>
                <send>
                    <endpoint>
                        <address uri="http://localhost:9000/services/SimpleStockQuoteService"/>
                    </endpoint>
                </send>
    </inSequence>


User could add your own mediation logic with whatever the mediators within the sequence..

Related post;
REST support in WSO2ESB - Introduction
REST Support in WSO2ESB - Handling JSON messages

3 comments:

  1. Hi,
    This is very nice.
    I was kind of lost until I came across this simple but useful guide to explore the world of ESB.

    Thanks
    Dhammika

    ReplyDelete
  2. hi Ratha,
    Thanks for this info and it definitely works for RESTful services. What about SOAP endpoints? If I want similar changes done to a SOAP endpoint, how is that possible?
    I raised a question around this topic in stackoverflow - http://stackoverflow.com/questions/17792427/wso2-esb-customuri-serviceuri-with-expression

    Thanks

    ReplyDelete
  3. Hi,
    After creating the proxy, I executed the following GETrequest url in the browser as u said.
    http://localhost:8280/services/RestProxy/getSimpleQuote?symbol=IBM
    But the browser shows an error "wrong number of arguments" . What is the problem here?

    ReplyDelete