Thursday, September 11, 2008

WADL - The REST Answerr of WSDL

Listing 1: WADL descriptor Example

<?xml version="1.0"?>
<application xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:yn="urn:yahoo:yn"
xmlns:ya="urn:yahoo:api"
xmlns="http://research.sun.com/wadl">

<grammars>
<include href="NewsSearchResponse.xsd"/>
<include href="NewsSearchError.xsd"/>
</grammars>

<resources base="http://api.search.yahoo.com/NewsSearchService/V1/">
<resource uri="newsSearch">
<method href="#search"/>
</resource>
</resources>

<method name="GET" id="search">
<request>
<query_variable name="appid" type="xsd:string" required="true"/>
<query_variable name="query" type="xsd:string" required="true"/>
<query_variable name="type" type="xsd:string"/>
<query_variable name="results" type="xsd:int"/>
<query_variable name="start" type="xsd:int"/>
<query_variable name="sort" type="xsd:string"/>
<query_variable name="language" type="xsd:string"/>
</request>
<response>
<representation mediaType="application/xml" element="yn:ResultSet"/>
<fault id="SearchError" status="400" mediaType="application/xml"
element="ya:Error"/>
</response>
</method>

</application>


To generate java client stub for this WADL contract, Sun provide following instruction:
java -jar wadl2java.jar -o gen-src -p com.techtarget.rest http://www.techtarget.com/newsrest.wadl


Listing 1: WADL descriptor Example 2

<?xml version="1.0" standalone="yes"?>
<application targetNamespace="urn:yahoo:yn"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:yn="urn:yahoo:yn"
xmlns:tns="urn:yahoo:yn"
xmlns:ya="urn:yahoo:api"
xmlns="http://research.sun.com/wadl">

<types>
<include
href="http://.../NewsSearchService/V1/NewsSearchResponse.xsd"/>
<include
href="http://.../Api/V1/error.xsd"/>
</types>

<resources>
<resource uri="http://.../NewsSearchService/V1/newsSearch">
<operationRef ref="tns:NewsSearch"/>
</resource>
</resources>

<operation name="NewsSearch" method="get">
<request>
<parameter name="appid" type="xsd:string" required="true"/>
<parameter name="query" type="xsd:string" required="true"/>
<parameter name="type" type="xsd:string"/>
<parameter name="results" type="xsd:int"/>
<parameter name="start" type="xsd:int"/>
<parameter name="sort" type="xsd:string"/>
<parameter name="language" type="xsd:string"/>
</request>
<response>
<representation mediaType="text/xml" element="yn:ResultSet">
<parameter name="totalResults"
type="xsd:nonNegativeInteger"
path="/ResultSet/@totalResultsAvailable"/>
<parameter name="resultsReturned"
type="xsd:nonNegativeInteger"
path="/ResultSet/@totalResultsReturned"/>
<parameter name="resultPosition"
type="xsd:nonNegativeInteger"
path="/ResultSet/@firstResultPosition"/>
<parameter name="results" path="/ResultSet/Result"/>
</representation>
<fault name="SearchError" status="400"
mediaType="text/xml" element="ya:Error">
<parameter name="msg" path="/Error/Message"
type="xsd:string"/>
</fault>
</response>
</operation>
</application>

For a full reference
WADL Spec: http://weblogs.java.net/blog/mhadley/wadl.pdf
WADL Schema: http://weblogs.java.net/blog/mhadley/wadl.xsd

HTTP methods (GET, POST, PUT, DELETE, HEAD) are specifically supported by the WADL schema

No comments: