Chapter 54. Extending JAX-RS Endpoints with Swagger Support
Abstract
CXF includes a SwaggerFeature (org.apache.cxf.jaxrs.swagger.SwaggerFeature
), which extends published JAX-RS endpoints with support for Swagger. Swagger is fundamentally a JSON-based format used to describe RESTful services, like its predecessors WSDL and WADL, but much easier to use. For a detailed description, see Swagger.
The SwaggerFeature is supported in both standalone CXF and in JBoss Fuse implementations.
54.1. Standalone CXF Implementations Copy linkLink copied to clipboard!
Overview Copy linkLink copied to clipboard!
This section describes how to use the SwaggerFeature in a standalone CXF implementation, in which REST services are defined inside WAR files and deployed to a standalone web server, such as Tomcat.
The project should be a Maven WAR project. After building and packaging it, you can deploy it to Tomcat by dropping the WAR file into the $TOMCAT_HOME/webapps
directory.
Enabling Swagger Copy linkLink copied to clipboard!
Enabling Swagger involves:
Modifying the XML file that defines the CXF service by adding the CXF class
org.apache.cxf.jaxrs.swagger.SwaggerFeature
to the<jaxrs:server>
definition:<jaxrs:server id="xx" address="/address" /> <jaxrs:features> <bean class="org.apache.cxf.jaxrs.swagger.SwaggerFeature"/> </jaxrs:features> </jaxrs:server>
<jaxrs:server id="xx" address="/address" /> <jaxrs:features> <bean class="org.apache.cxf.jaxrs.swagger.SwaggerFeature"/> </jaxrs:features> </jaxrs:server>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow See Example 54.1, “Example Swagger-enabled Spring configuration” for an example of a Spring XML file for a Swagger-enabled CXF service.
In the REST resource class:
Importing the Swagger API annotations for each annotation required by the service:
import com.wordnik.swgger.annotations.Api*
where * = Api, ApiOperation, ApiParam, ApiResponse, ApiResponses, and so on.
Adding Swagger annotations to the JAX-RS annotated endpoints (@PATH, @PUT, @POST, @GET, @Produces, @Consumes, @DELETE, @PathParam, and so on).
See Example 54.2, “Example REST resource with Swagger annotations” for an example of a Swagger-annotated REST resource class.
Configuration examples Copy linkLink copied to clipboard!
Example 54.1. Example Swagger-enabled Spring configuration
This example shows only the relevant parts of a Swagger-annotated REST resource class.
Example 54.2. Example REST resource with Swagger annotations
54.2. JBoss Fuse CXF Implementations Copy linkLink copied to clipboard!
Overview Copy linkLink copied to clipboard!
This section describes how to use the SwaggerFeature in JBoss Fuse CXF implementations, in which REST services are defined inside JAR files and deployed to a JBoss Fuse container or a fabric8 container.
Your JBoss 7.1 installation contains a Quickstart (installDir/quickstarts/cxf/rest/
) that demonstrates how to create a RESTful (JAX-RS) web service using CXF and how to enable Swagger and annotate the JAX-RS endpoints.
Enabling Swagger Copy linkLink copied to clipboard!
Enabling Swagger involves:
Modifying the XML file that defines the CXF service by adding the CXF class (
io.fabric8.cxf.endpoint.SwaggerFeature
) to the<jaxrs:server>
definition.For example:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow In the REST resource class:
Importing the Swagger API annotations for each annotation required by the service:
import com.wordnik.swagger.annotations.Api*
where * = Api, ApiOperation, ApiParam, ApiResponse, ApiResponses, and so on.
For details, see https://github.com/swagger-api/swagger-core/wiki/Annotations.
Adding Swagger annotations to the JAX-RS annotated endpoints (@PATH, @PUT, @POST, @GET, @Produces, @Consumes, @DELETE, @PathParam, and so on).
For an example, see Example 54.2, “Example REST resource with Swagger annotations”.
Building and Deploying the Rest quickstart Copy linkLink copied to clipboard!
See the README file in your JBoss Fuse 7.1 installDir/quickstarts/cxf/rest/
directory for instructions on how to build and deploy the rest quickstart.