Chapter 54. Extending JAX-RS Endpoints with Swagger Support
Abstract
The CXF Swagger2Feature (org.apache.cxf.jaxrs.swagger.Swagger2Feature
) allows you to generate Swagger 2.0 documents by extending published JAX-RS service endpoints with a simple configuration.
The Swagger2Feature is supported in both Spring Boot and Karaf implementations.
54.1. Swagger2Feature options Copy linkLink copied to clipboard!
You can use the following options in Swagger2Feature.
Name | Description | Default |
---|---|---|
|
The context root path+ (see also the | null |
| Your contact information+ | |
| A description+ | "The Application" |
| A security filter+ | null |
| The host and port information+ | null |
|
Excludes specific paths when scanning all resources (see the | null |
| The license+ | "Apache 2.0 License" |
| The license URL+ | |
|
When generating | false |
| A list of comma separated package names where resources must be scanned+ | A list of service classes configured at the endpoint |
| Runs the feature as a filter | false |
| Generates the swagger documentation+ | true |
|
Scans all resources including non-annotated JAX-RS resources (see also the | false |
| The protocol schemes+ | null |
| Swagger UI configuration | null |
| The terms of service URL+ | null |
| The title+ | "Sample REST Application" |
|
Prevents Swagger from caching the value of the | false |
| The version+ | "1.0.0" |
+ The option is defined in Swagger’s BeanConfig
++ The option is defined in Swagger’s ReaderConfig
54.2. Karaf Implementations Copy linkLink copied to clipboard!
This section describes how to use the Swagger2Feature in which REST services are defined inside JAR files and deployed to a Fuse on Karaf container.
54.2.1. Quickstart example Copy linkLink copied to clipboard!
You can download Red Hat Fuse quickstarts
from the Fuse Software Downloads page.
The Quickstart zip file contains a /cxf/rest/
directory for a quickstart that demonstrates how to create a RESTful (JAX-RS) web service using CXF and how to enable Swagger and annotate the JAX-RS endpoints.
54.2.2. 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.Swagger2Feature
) to the<jaxrs:server>
definition.For an example, see Example 55.4 Example XML file.
In the REST resource class:
Importing the Swagger API annotations for each annotation required by the service:
import io.swagger.annotations.*
import io.swagger.annotations.*
Copy to Clipboard Copied! Toggle word wrap Toggle overflow where * =
Api
,ApiOperation
,ApiParam
,ApiResponse
,ApiResponses
, and so on.For details, go to
https://github.com/swagger-api/swagger-core/wiki/Annotations
.For an example, see Example 55.5 Example Resource class.
-
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 55.5 Example Resource class.
Example 55.4 Example XML file
Example 55.5 Example Resource class
54.3. Spring Boot Implementations Copy linkLink copied to clipboard!
This section describes how to use the Swagger2Feature in Spring Boot.
54.3.1. Quickstart example Copy linkLink copied to clipboard!
The Quickstart example (https://github.com/fabric8-quickstarts/spring-boot-cxf-jaxrs
) demonstrates how you can use Apache CXF with Spring Boot. The Quickstart uses Spring Boot to configure an application that includes a CXF JAX-RS endpoint with Swagger enabled.
54.3.2. Enabling Swagger Copy linkLink copied to clipboard!
Enabling Swagger involves:
In the REST application:
Importing Swagger2Feature:
import org.apache.cxf.jaxrs.swagger.Swagger2Feature;
import org.apache.cxf.jaxrs.swagger.Swagger2Feature;
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Adding Swagger2Feature to a CXF endpoint:
endpoint.setFeatures(Arrays.asList(new Swagger2Feature()));
endpoint.setFeatures(Arrays.asList(new Swagger2Feature()));
Copy to Clipboard Copied! Toggle word wrap Toggle overflow For an example, see Example 55.1 Example REST application.
In the Java implementation file, importing the Swagger API annotations for each annotation required by the service:
import io.swagger.annotations.*
import io.swagger.annotations.*
Copy to Clipboard Copied! Toggle word wrap Toggle overflow where * =
Api
,ApiOperation
,ApiParam
,ApiResponse
,ApiResponses
, and so on.For details, see
https://github.com/swagger-api/swagger-core/wiki/Annotations
.For an example, see Example 55.2 Example Java implementation file.
In the Java file, 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 55.3 Example Java file.
Example 55.1 Example REST application
Example 55.2 Example Java implementation file
Example 55.3 Example Java file
54.4. Accessing Swagger Documents Copy linkLink copied to clipboard!
When Swagger is enabled by Swagger2Feature, the Swagger documents are available at the location URL constructed of the service endpoint location followed by /swagger.json
or /swagger.yaml
.
For example, for a JAX-RS endpoint that is published at http://host:port/context/services/
where context
is a web application context and /services
is a servlet URL, its Swagger documents are available at http://host:port/context/services/swagger.json
and http://host:port/context/services/swagger.yaml
.
If Swagger2Feature is active, the CXF Services page links to Swagger documents.
In the above example, you would go to http://host:port/context/services/services
and then follow a Swagger link which returns a Swagger JSON document.
If CORS support is needed to access the definition from a Swagger UI on another host, you can add the CrossOriginResourceSharingFilter
from cxf-rt-rs-security-cors
.
54.5. Accessing Swagger through a reverse proxy Copy linkLink copied to clipboard!
If you want to access a Swagger JSON document or a Swagger UI through a reverse proxy, set the following options:
Set the
CXFServlet use-x-forwarded-headers
init parameter to true.In Spring Boot, prefix the parameter name with
cxf.servlet.init
:cxf.servlet.init.use-x-forwarded-headers=true
cxf.servlet.init.use-x-forwarded-headers=true
Copy to Clipboard Copied! Toggle word wrap Toggle overflow In Karaf, add the following line to the
installDir/etc/org.apache.cxf.osgi.cfg
configuration file:cxf.servlet.init.use-x-forwarded-headers=true
cxf.servlet.init.use-x-forwarded-headers=true
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Note: If you do not already have an
org.apache.cxf.osgi.cfg
file in youretc
directory, you can create one.
If you specify a value for the Swagger2Feature
basePath
option and you want to prevent Swagger from caching thebasePath
value, set the Swagger2FeatureusePathBasedConfig
option to TRUE:<bean class="org.apache.cxf.jaxrs.swagger.Swagger2Feature"> <property name="usePathBasedConfig" value="TRUE" /> </bean>
<bean class="org.apache.cxf.jaxrs.swagger.Swagger2Feature"> <property name="usePathBasedConfig" value="TRUE" /> </bean>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow