What is a WADL file ?

WADL file is the WSDL file for a REST webservice. All servcies exposed on REST are described following the WADL specification : http://www.w3.org/Submission/wadl/

With the help of a fantastic post on stackoverflow, here is a sample of using the magic maven jersey plugin:

Reference: http://stackoverflow.com/questions/9486835/wadl-generation-tool#answer-14742322

Add the plugin

First, you need to add the declration to your maven pom.xml :

<build><plugins>...    <plugin>        <groupId>com.sun.jersey.contribs</groupId>        <artifactId>maven-wadl-plugin</artifactId>        <version>1.17</version>        <executions>            <execution>                <id>generate</id>                <goals>                    <goal>generate</goal>                </goals>                <phase>${javadoc-phase}</phase>            </execution>        </executions>        <configuration>            <wadlFile>${basedir}/src/main/resources/application.wadl            </wadlFile>            <formatWadlFile>true</formatWadlFile>            <baseUri>*http://SERVER_URL:SERVER_PORT/PATH_TO_MY_REST_SERVICE*</baseUri>            <packagesResourceConfig>                <param>*PACKAGE_OF_MY_JAVA_REST_CODE*</param>            </packagesResourceConfig>            <wadlGenerators>                <wadlGeneratorDescription>                    <className>com.sun.jersey.server.wadl.generators.WadlGeneratorApplicationDoc                    </className>                    <properties>                        <property>                            <name>applicationDocsFile</name>                            <value>${basedir}/src/main/resources/application-doc.xml</value>                        </property>                    </properties>                </wadlGeneratorDescription>                <wadlGeneratorDescription>                    <className>com.sun.jersey.server.wadl.generators.WadlGeneratorGrammarsSupport                    </className>                    <properties>                        <property>                            <name>grammarsFile</name>                            <value>${basedir}/src/main/resources/application-grammars.xml</value>                        </property>                    </properties>                </wadlGeneratorDescription>            </wadlGenerators>        </configuration>    </plugin></plugins></build>

Create doc and gramar files

To perform such generation, you need to add 2 files to this maven plugin:

A Doc description file, and a grammar description file.

The doc description file

In your src/java/resources path, create a application-doc.xml file

<?xml version="1.0" encoding="UTF-8"?><applicationDocs targetNamespace="http://wadl.dev.java.net/2009/02">    <doc xml:lang="en" title="A message in the WADL">This is added to the start of the generated application.wadl</doc></applicationDocs>

The grammars file

The in the same path, create a application-grammars.xml file:

<?xml version="1.0" encoding="UTF-8" ?><grammars xmlns="http://wadl.dev.java.net/2009/02" />

Run plugin

Then, with the magic of pmaven, just run the following command in the path of your project

mvn compile com.sun.jersey.contribs:maven-wadl-plugin:generate

now in the src/java/resources duirectory, a new file application.wadl has just been created.

sample:

<?xml version="1.0" encoding="UTF-8"?><application xmlns="http://wadl.dev.java.net/2009/02">        <doc jersey:generatedBy="Jersey: 1.17 01/17/2013 03:31 PM"        xmlns:jersey="http://jersey.java.net/"/>    <doc xml:lang="en"        title="A message in the WADL"><![CDATA[This is added to the start of the generated application.wadl]]>    </doc>    <grammars/>    <resources base="http://localhost:8080/restapi"/>    ...</application>

Conclusion

It’s very easy to create such file thanks to the Jersey maven plugin. so don’t hesitate to use such things !

Have Fun !

McGivrer

Publicités