Using WSO2 ESB HTTP Endpoints to define Restful Endpoints

When we consider about the endpoints which has been allowed in WSO2 ESB there are several as follows

  • Address Endpoint
  • Dynamic Load-balance Endpoint
  • Failover Group
  • HTTP Endpoint
  • WSDL Endpoint
  • Load-balance Group
  • Indirect and Resolving Endpoints
  • Default Endpoint
  • Template Endpoint
  • Recipient List Endpoint

In this post I am going to take you through, how to use HTTP Endpoints to define Restful Endpoints. You can use Uri Templates to achieve this. There we can use the variables to retrieve the values of the uri at run time.

In order to demonstrate this I am going to define an api which I am going to access through an HTTP Endpoint and a proxy service.



<endpoint xmlns="http://ws.apache.org/ns/synapse" name="TestHttpEP">
   <http uri-template="http://localhost:8280/tutorial/{uri.var.param}/{uri.var.val}" method="get">
      <suspendOnFailure>
         <progressionFactor>1.0</progressionFactor>
      </suspendOnFailure>
      <markForSuspension>
         <retriesBeforeSuspension>0</retriesBeforeSuspension>
         <retryDelay>0</retryDelay>
      </markForSuspension>
   </http>
</endpoint>

In the above defined HTTP Endpoint, uri-template attribute has been set to the resource defined in the bellow API. Also You can see the variables inside the curly Braces. These variables get the defined property values at the run time and generate the required uri. These properties have been defined in the proxy service's in sequence in which I am invoking.



<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="TestHttpProxy"
       transports="https,http"
       statistics="disable"
       trace="disable"
       startOnLoad="true">
   <target>
      <inSequence>
         <property name="uri.var.param" value="parameter" scope="default" type="STRING"/>
         <property name="uri.var.val" value="1234" scope="default" type="STRING"/>
         <property name="NO_KEEPALIVE" value="true" scope="axis2"/>
         <log>
            <property name="In_Seq" value="Hit In Sequence"/>
         </log>
         <send>
            <endpoint key="TestHttpEP"/>
         </send>
      </inSequence>
      <outSequence>
         <log>
            <property name="Out_Seq" value="Hit Out Sequence"/>
         </log>
         <send/>
      </outSequence>
   </target>
   <description/>
</proxy>



<api xmlns="http://ws.apache.org/ns/synapse" name="testApi" context="/tutorial">
   <resource methods="GET" uri-template="/test/val" faultSequence="fault">
      <inSequence>
         <log>
            <property name="IN_SEQ_LOG" value="HIT_IN_SEQ"/>
         </log>
         <payloadFactory media-type="xml">
            <format>
               <test xmlns="">TEST</test>
            </format>
            <args/>
         </payloadFactory>
         <respond/>
      </inSequence>
      <outSequence>
         <log>
            <property name="OUT_SEQ_LOG" value="OUT_SEQ_HIT"/>
         </log>
      </outSequence>
   </resource>
</api>

Now invoke the proxy service through any rest client and you will see the response sent through the API as follows.
<test xmlns="">TEST</test>

Comments

Popular posts from this blog

Enabling Custom SSL Profiles in WSO2 ESB 4.8.1

Integrating WSO2 ESB Connectors in real world integration Scenarios