Enterprise Integration Zone is brought to you in partnership with:

Madhuka is Software Engineer and currently works on the Jaggery, UES, WSO2 Gadget Server team. He holds a B.Sc (Hons) degree in Information Technology from the Faculty of Information Madhuka is a DZone MVB and is not an employee of DZone and has posted 50 posts at DZone. You can read more from them at their website. View Full User Profile

REST APIs with WSO2 ESB

02.10.2014
| 3430 views |
  • submit to reddit
Since Representational State Transfer (REST)  is becoming more popular, this post illustrates the process of getting the REST api from wso2 ESB concepts through the XML.
<api xmlns="http://ws.apache.org/ns/synapse" name="MY_API" context="/student">
           <resource methods="GET" url-mapping="/list" inSequence="sequence1" outSequence="sequence2"/>
</api>

Steps of the Flow

  1. When request is dispatched to a resource it will be mediated through the in-sequence of the resource
  2. At the end of the in-sequence the request will be forwarded to a back-end service for further processing
  3. Responses coming from the back-end system are mediated through the out-sequence of the resource
  4. Fault-sequence can be defined to handle any errors while mediating a message through a resource

API context

Each API is anchored at a user-defined URL context same as web application concept. Therefore  API will only process requests that fall under its URL context. (you can defined hostname and/or a port number)

example

if a particular API is anchored at the context “/student”, only HTTP requests whose URL path starts with “/student” will be handled by that API(MY_API).

Resource mapping to URL mapping or URI template

A resource can be associated with a user-defined URL mapping or URI template.
A resource can also be bound to a specific subset of HTTP verbs and header values
eg: 

  • GET /test/foo/bar
  • GET /test/foo/a?arg1=hello

URL mapping can have Path mappings (/student/*), Extension mappings (*.html) or Exact mappings ( /student/james)

URI templates can be /student/{grade}/ OR /student/{grade}/{studentId}

<api name="MY_API2" context="/student">

<resource url-mapping="/{grade}" methods="GET" inSequence="sequence3" outSequence="sequence4"/>

<resource uri-template="/{grade}/{studentId}" methods="GET POST" inSequence="sequence5" outSequence="sequence6"/>

</api> 

Try out with ESB

The ESB provides access to the exact values of the template variables through message context properties as below

<log level="custom">
   <property name="Grade" expression="get-property('uri.var.grade’)"/>
   <property name="StudentId" expression="get-property('uri.var.studentId’)"/>
</log>

Demo Code

API

<api xmlns="http://ws.apache.org/ns/synapse" name="MY_API" context="/student">
   <resource methods="GET" uri-template="/{grade}/{studentId}" inSequence="sequence1" outSequence="sequence2"/>
</api>

sequence

<sequence xmlns="http://ws.apache.org/ns/synapse" name="sequence1">
   <log level="custom">
      <property xmlns:ns="http://org.apache.synapse/xsd" name="Grade" expression="get-property('uri.var.grade')"/>
      <property xmlns:ns="http://org.apache.synapse/xsd" name="StudentId" expression="get-property('uri.var.studentId')"/>
   </log>
</sequence>

Calling URL:: http://localhost:8280/student/grade1/12

image

Console log is

[2014-01-07 19:02:44,076]  INFO - LogMediator Grade = grade1, StudentId = 12

image

Published at DZone with permission of Madhuka Udantha, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)