WADL (Web Application Description Language) is an XML document, which describes what resources of an application can be accessed by a client. WADL is considered as the REST equivalent of SOAP's WSDL.
Let's see an example how to access WADL in a JAX-RS application.
Example
Let's create multiple resource methods:
@Path("/")
public class MyResource {
@Path("/test")
@GET
public Response handle() {
Response r = Response.ok("test with GET, body content")
.build();
return r;
}
.............
@Path("/test2")
@GET
public Response handle2() {
Response r = Response.ok("test2 with GET, body content")
.build();
return r;
}
.............
@Path("/test3")
@GET
public Response handle5() {
Response r = Response.ok("test3 with GET, body content")
.build();
return r;
}
.............
@Path("/test4")
@DELETE
public Response handle7() {
Response r = Response.ok("test4 with DELETE, body content")
.build();
return r;
}
.............
}
To try examples, run embedded tomcat (configured in pom.xml of example project below):
mvn tomcat7:run-war
JAX-RS client
WADL can be retrieved from URI application.wadl as shown in this example.
public class MyClient {
public static void main(String[] args) {
Client client = ClientBuilder.newClient();
WebTarget target =
client.target("http://localhost:8080/application.wadl" );
Response response = target.request()
.get();
System.out.printf("status: %s%n", response.getStatus());
MultivaluedMap<String, Object> headers = response.getHeaders();
System.out.println("-- response headers --");
headers.entrySet().forEach(e-> System.out.printf("%s: %s%n", e.getKey(), e.getValue()));
System.out.printf("-- response body --%n%s%n", response.readEntity(String.class));
}
} Outputstatus: 200 -- response headers -- Last-modified: [Thu, 08 Jun 2017 16:15:01 CDT] Server: [Apache-Coyote/1.1] Content-Length: [2431] Date: [Thu, 08 Jun 2017 21:15:43 GMT] Content-Type: [application/vnd.sun.wadl+xml] -- response body -- <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <application xmlns="http://wadl.dev.java.net/2009/02"> <doc xmlns:jersey="http://jersey.java.net/" jersey:generatedBy="Jersey: 2.25.1 2017-01-19 16:23:50"/> <doc xmlns:jersey="http://jersey.java.net/" jersey:hint="This is simplified WADL with user and core resources only. To get full WADL with extended resources use the query parameter detail. Link: http://localhost:8080/application.wadl?detail=true"/> <grammars/> <resources base="http://localhost:8080/"> <resource path="/"> <resource path="/test"> <method id="handle" name="GET"> <response> <representation mediaType="*/*"/> </response> </method> </resource> <resource path="/test3"> <method id="handle5" name="GET"> <response> <representation mediaType="*/*"/> </response> </method> <method id="handle6" name="OPTIONS"> <response> <representation mediaType="*/*"/> </response> </method> </resource> <resource path="/test2"> <method id="handle2" name="GET"> <response> <representation mediaType="*/*"/> </response> </method> <method id="handle3" name="POST"> <response> <representation mediaType="*/*"/> </response> </method> <method id="handle4" name="HEAD"> <response> <representation mediaType="*/*"/> </response> </method> </resource> <resource path="/test4"> <method id="handle8" name="PUT"> <response> <representation mediaType="*/*"/> </response> </method> <method id="handle7" name="DELETE"> <response> <representation mediaType="*/*"/> </response> </method> </resource> </resource> </resources> </application>
Since WADL can be accessed via a simple GET request, we can also access it in a web browser.
Example ProjectDependencies and Technologies Used: - jersey-server 2.25.1: Jersey core server implementation.
- jersey-container-servlet 2.25.1: Jersey core Servlet 3.x implementation.
- JDK 1.8
- Maven 3.3.9
|