Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

 

Span
stylefont-size:2em;font-weight:bold
JAX-RS Services Description
 

 

 

 

Table of Contents

Swagger

Swagger-First Development

Swagger Auto Generation

Please see the Swagger2Feature page for more information

WADL

 

CXF JAX-RS supports (Web Application Description Language|http://www.w3.org/Submission/wadl] (WADL).
Users can use WADL documents to generate the initial code and have WADL auto-generated on demand.

...

Overview

WADL is a resource-centric description language which has been designed to facilitate the modeling, description and testing of
RESTful Web applications. Please check the official page for more information, this section provides a brief overview of main WADL constructs.

Basic example

A top level WADL document element is called "application". Usually it may contain a "grammars" section and "resources" element with one or more top-level "resource" elements, with each one representing a specific root resource, for example:

...

Also note that the resource with the "/books" path has another child resource with the "/bookstore/{id}" path, but it could've had a "/books/bookstore/{id}" path instead and no child resource.

WADL with references

Basic WADL example in the previous section shows a "grammars" section with the inlined schema, as well as a "resource" description with the "/bookstore/{id}" path listed twice, as an immediate child of the "resources" and as a child of the "resource" element with the "/books" path.

...

Note that a book.xsd schema resource located in the 'schemas' path relative to the location of this WADL document is referenced using wadl:include element. Abstract resource type "bookResource" is declared as an immediate child of wadl:application and is linked to concrete resource elements using a "#bookResource" reference.

Sharing declarations between multiple WADLs

WADL references allow for having WADL documents with abstract declarations only and concrete WADLs referencing them, thus making it possible to reuse resource declarations in different web application descriptions.

...

New: Starting from CXF 2.5.0 and 2.4.4 all WADL elements may link to top-level local declarations, see this example.

WADL-first Development

CXF 2.4.1 introduces a wadl2java code generator and cxf-wadl2java-plugin Maven plugin which can be used to generate server and client JAX-RS code and speed up the transition between modeling and implementation stages.

...

Classes generated from schemas will have a package name derived from a given schema target namespace by default. wadltojava tool lets customize it as well.

wadl2java command line tool

Running wadltojava from the command line will produce:

...

In some cases, example when describing JSON arrays, you may want to have an explicit collection of types defined in schema generated. In this case use -tMap or -repMap option with a value such as "List..MyType".

 

JAXB customizations

At the moment it is possible to apply external JAXB customizations to WADL grammars however it is not possible yet to restrict a given customization to a specific WADL document or explicitly inlined schema. Linking binding to external schemas works, for example, the following bindings file can be used:

Code Block
xml
xml
<jaxb:bindings version="2.0"
	xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
	xmlns:xs="http://www.w3.org/2001/XMLSchema"
	schemaLocation="schemas/book.xsd"
	node="//xs:complexType[@name='book2']/xs:sequence/xs:element[@name='id']">
	<jaxb:property name="book2Id"/>
</jaxb:bindings>

wadl2java Maven plugin

If you need the code generated during Maven build then the following plugin can be used:

...

Code Block
xml
xml
<wadlOptions>
	<wadlOption>
		<wadl>$\{basedir}/src/main/wadl/bookStore.wadl</wsdl>
                <extraargs>
                    <extraarg>-impl</extraarg>
                    <extraarg>-verbose</extraarg>
                </extraargs>
	</wadlOption>
</wadlOptions>

Integration

Two options are available to developers who wish to integrate CXF JAX-RS WADLToJava code generator.
First option is to pass the collected options directly to a wadltojava process.

...

Please see CXF source for more details and org.apache.cxf.tools.wadlto.jaxrs.JAXRSContainerTest in particular.

External WADL documents and JAXRS endpoints.

External WADL documents can be linked to from jaxrs:server endpoints using newly introduced "docLocation" attribute, for example:

...

If external WADL documents include external schemas and jaxrs endpoints need to have the schema validation enabled, then those schemas can be referenced in the jaxrs:schemaLocations section as well.

WADL Auto Generation at Runtime

Note that in CXF 3.0.0 WADL Generator code has been moved to

...

CXF 2.4.0: org.apache.cxf.jaxrs.ext.Description and org.apache.cxf.jaxrs.ext.xml.XMLName have been moved to org.apache.cxf.jaxrs.model.wadl package given that their purpose is to improve the WADL generation. Also, org.apache.cxf.jaxrs.model.wadl.WadlElement has been renamed to 'ElementClass'.

Documenting resource classes and methods in generated WADL

WADL documents can include doc fragments.

...

Code Block
java
java
@POST
@Path("books/{bookid}")
@Description(value = "Resource", target = DocTarget.RESOURCE),
public Book addBook(@Description("book id") @PathParam("id") Long id, @Description("New Book") Book book) {...}

@GET
@Path("books/{bookid}")
public Book addBook(@Description("book id") @PathParam("id") Long id) {...}

Support for Javadoc

In CXF 3.0.0 one can get the Javadoc documentation copied to WADL being auto-generated at runtime.

...

JavaDocProvider parses the generated Javadoc HTML pages and scrapes the documentation. See Java to Wadl section on the alternative approach for supporting Javadoc.

Customizing WADL Generation

One can register a custom WadlGenerator as a jaxrs:provider. The custom generator can extend the default
org.apache.cxf.jaxrs.model.wadl.WadlGenerator or register a default one with one of the following properties set.

...

An ElementClass annotation can help with representing JAX-RS Response elements in the generated WADL.

Representing explicit JAXB collections

Starting from CXF 2.5.5 and 2.6.2 it is possible to get explicit collections represented in generated WADL grammar sections and have WADL representations linking to these schema elements. Note it is only possible for JAXB collections, for example:

...

Given the above example, WADLGenerator will attempt to add a 'books' element to the generated schema with the targetNamespace set to "http://books". This 'books' element will have a sequence of elements linking to a type representing the "Book" class.

Representing external schemas and non JAXB types

By default, the WADL grammar section will be properly generated if resource methods accept or return JAXB types.

...

If no JAXB is used then you can attach an XMLName annotation to method input or output types. Alternatively, you can register an instance of ElementQNameResolver with the WadlGenerator which will be used for creating wadl:representation/@element values.

Changing the base address

Starting from CXF 2.6.2 it is possible to affect the base address specified in the auto-generated WADL (in wadl:resources/@base attribute).
WADLGenerator can be indirectly configured by setting a jaxrs:server/@publishedEndpointUrl attribute, similarly to the way CXF WSDL generator can be configured by setting a jaxws:endpoint/@publishedEndpointUrl attribute.

 

java2wadl Maven plugin

CXF 3.0.0 and 2.7.11 introduce java2wadl plugin for generating WADL at the build time:

...

Note that Javadoc can be properly supported by enabling the "parsejavadoc" execution and a docProvider property.

WADL Transformations

Starting from CXF 3.0.4 it is possible to configure WADLGenerator with a 'stylesheetReference' property pointing to a local XSLT template. 

...

This feature can help with further enhancing the generated WADL XML with some simple transformations (example, adding some information to WADL XML that is not possible to get from the annotated JAX-RS resources) or convert it to HTML.

Service listings and WADL queries

Links to WADL instances for RESTful endpoints are available from {base endpointaddress}/services, in addition to SOAP endpoints if any.

...

You can easily customize what Content-Type is set by either using a WadlGenerator 'defaultMediaType' property or using the extension mappings and adding a .wadl at the end of request URI instead of using a _wadl query: "http://localhost:8080/store/books/orders/fiction.wadl", where extensions will map a 'wadl' to 'application/vnd.sun.wadl+xml' or indeed to 'application/xml'.

WADL in JSON format

Use a "?_wadl&_type=json" or something like "fiction.wadl", where extensions will map a 'wadl' to 'application/json' in order to get a WADL JSON representations, please see this blog entry for more information.

...