JAXRS Services DescriptionPage edited by Sergey BeryozkinChanges (1)
Full ContentJAX-RS Services Description CXF JAX-RS supports WADL. WADL-first Developmentwadl2java command line toolwadl2java Maven pluginIf you need the code generated during the Maven build then the following plugin can be used: <groupId>org.apache.cxf</groupId> <artifactId>cxf-wadl2java-plugin</artifactId> <version>2.4.1</version>
Starting from CXF 2.4.1 and 2.3.5 the following properties are also supported:
Representing external schemas and non JAXB typesBy default, the WADL grammar section will be properly generated if resource methods accept or return JAXB types. Even when you do use JAXB, the JAXB types may have been generated from the external schema so having WADLGenerator attempting to recreate the original schema may not work well. To have a generated WADL referencing the original schema(s) please set a 'schemaLocations' list property (programmatically or from Spring) : WadlGenerator wg = new WadlGenerator(); wg.setSchemaLocations(Collections.singletonList("classpath:/book.xsd")); In this case the grammar section will have the 'book.xsd' schema inlined. If this schema imports other schemas then the imports with relative URIs will be replaced by the absolute URIs based on the current endpoint's base address. For example, if the endpoint address is "http://somehost/bar" and the 'book.xsd' imports "foo/book1.xsd" then the published WADL will contain an "http://somehost/bar/foo/book1.xsd". At the moment a custom RequestHandler filter will have to be registered to serve resources such as "http://somehost/bar/foo/book1.xsd" which can 'calculate' which resource is required get the absolute request URI and comparing it with the base URI, possibly with the help of the injected JAXRS UriInfo context. Alternatively, resources such as book1.xsd may be served by CXFServlet itself (see the Redirection with CXFServlet) TODO : add ignoreImports flag so that users can list root and imported schemas in "schemaLocations" and have them all inlined. Note that the root schema such as "book.xsd" is inlined - you can have it referenced only by setting an 'externalLinks' list property. This will work very well when the "book.xsd" is indeed available at the external URI, but this property can also be used to avoid the local schemas being inlined. Moreover, the use of JAXB will not be required. The result will look like this: <wadl:grammars> <wadl:include href="" class="code-quote">"http://books.xsd"/> </wadl:grammars> Note that "schemaLocations" and "externalLinks" properties differ in that the schemas referenced by the former one are inlined. You can also customize the way schema elements are referenced. When WADLGenerator creates WADL representation elements (representing resource method input or output types) it will be able to link to schema elements provided a given type is actually a JAXB one, so the result may look like this :
<!--
thebook2 element is declared in a schema inlined in or referenced from the grammar section
prefix1 is bound to that schema's target namespace and is declared at the wadl:application element
-->
<representation mediaType="application/xml" element="prefix1:thebook2"/>
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. An ElementClass annotation can help with representing JAX-RS Response elements in the generated WADL. Generating the client code from WADL at runtimeIf you register an org.apache.cxf.jaxrs.ext.codegen.CodeGeneratorProvider with a jaxrs endpoint and issue a '_code' query to it then you will get back an XHTML page containing the link to a zipped client source code which you can download and start customizing. Test/development servers can host such a provider to help implementers get started with working on the client code asap. Internally, before the code gets generated, WADL will be generated first. The archive will include JAXB generated classes from a WADL grammar section plus the proxy based client code for accessing root and sub resources. The WebClient based code can not be generated just yet but one can request that only a WADL grammar section is processed by adding a '_codeType=grammar' query and easily adding a WebClient-based code. Here is how to get the archive programmatically : WebClient wc = WebClient.create("http://localhost:9080/petstore"); XMLSource source = wc.query("_code").query("_os", getOs()).get(XMLSource.class); String link = source.getValue("ns:html/ns:body/ns:ul/ns:a/@href", Collections.singletonMap("ns","http://www.w3.org/1999/xhtml")); // download a zip file WebClient wcZip = WebClient.create(link); InputStream is = wcZip.accept("application/zip").get(InputStream.class); // unzip and compile it Please see a testPetStore test for more details. Hiding links to JAXRS endpoints from the services pageIn some cases you may not want the users to see the links to some JAXRS endpoints. For example, if you have an AtomPullServer endpoint collecting the log entries for a number of application endpoints, you may not want to see AtomPullServer endpoint being listed among the endpoints representing the actual application and which users are actually interested in. If so then adding an "org.apache.cxf.endpoint.private" boolean property with a value "true" will do the trick; note the same property can be used by jaxws endpoints.
Change Notification Preferences
View Online
|
View Changes
|
Add Comment
|
- [CONF] Apache CXF Documentation > JAXRS Services Description confluence
- [CONF] Apache CXF Documentation > JAXRS Services Descri... confluence
- [CONF] Apache CXF Documentation > JAXRS Services Descri... confluence
- [CONF] Apache CXF Documentation > JAXRS Services Descri... confluence
- [CONF] Apache CXF Documentation > JAXRS Services Descri... confluence
- [CONF] Apache CXF Documentation > JAXRS Services Descri... confluence
