Over a million developers have joined DZone.

REST APIs with WSO2 ESB

· Integration Zone

Build APIs from SQL and NoSQL or Salesforce data sources in seconds. Read the Creating REST APIs white paper, brought to you in partnership with CA Technologies.

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

The Integration Zone is brought to you in partnership with CA Technologies.  Use CA Live API Creator to quickly create complete application backends, with secure APIs and robust application logic, in an easy to use interface.

Topics:

Published at DZone with permission of Madhuka Udantha, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}