Chapter 54. Extending JAX-RS Endpoints with OpenAPI Support
Abstract
The CXF OpenApiFeature (org.apache.cxf.jaxrs.openapi.OpenApiFeature
) allows you to generate OpenAPI documents by extending published JAX-RS service endpoints with a simple configuration.
The OpenApiFeature is supported in both Spring Boot and Karaf implementations.
54.1. OpenApiFeature options Copy linkLink copied to clipboard!
You can use the following options in OpenApiFeature.
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 OpenAPI documentation+ | true |
|
Scans all resources including non-annotated JAX-RS resources (see also the | false |
| The protocol schemes+ | null |
| OpenAPI UI configuration | null |
| The terms of service URL+ | null |
| The title+ | "Sample REST Application" |
|
Prevents OpenAPI from caching the value of the | false |
| The version+ | "1.0.0" |
+ The option is defined in OpenAPIs’s BeanConfig
++ The option is defined in OpenAPI’s ReaderConfig
54.2. Karaf Implementations Copy linkLink copied to clipboard!
This section describes how to use the OpenApiFeature 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 OpenAPI and annotate the JAX-RS endpoints.
54.2.2. Enabling OpenAPI Copy linkLink copied to clipboard!
Enabling OpenAPI involves:
Modifying the XML file that defines the CXF service by adding the CXF class (
org.apache.cxf.jaxrs.openapi.OpenApiFeature
) to the<jaxrs:server>
definition.For an example, see Example 55.4 Example XML file.
In the REST resource class:
Importing the OpenAPI annotations for each annotation required by the service:
import io.openapi.annotations.*
import io.openapi.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/openapi-api/openapi-core/wiki/Annotations
.For an example, see Example 55.5 Example Resource class.
-
Adding OpenAPI 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.
Note that for OpenAPI 3 implementations, use the OpenApiFeature(org.apache.cxf.jaxrs.openapi.OpenApiFeature
).
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 OpenAPI Documents Copy linkLink copied to clipboard!
When OpenAPI is enabled by OpenApiFeature, the OpenAPI documents are available at the location URL constructed of the service endpoint location followed by /openapi.json
or /openapi.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 OpenAPI documents are available at http://host:port/context/services/openapi.json
and http://host:port/context/services/openapi.yaml
.
If OpenApiFeature is active, the CXF Services page links to OpenAPI documents.
In the above example, you would go to http://host:port/context/services/services
and then follow a link which returns an OpenAPI JSON document.
If CORS support is needed to access the definition from an OpenAPI UI on another host, you can add the CrossOriginResourceSharingFilter
from cxf-rt-rs-security-cors
.
54.5. Accessing OpenAPI through a reverse proxy Copy linkLink copied to clipboard!
If you want to access an OpenAPI JSON document or an OpenAPI 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 OpenApiFeature
basePath
option and you want to prevent OpenAPI from caching thebasePath
value, set the OpenApiFeatureusePathBasedConfig
option to TRUE:<bean class="org.apache.cxf.jaxrs.openapi.OpenApiFeature"> <property name="usePathBasedConfig" value="TRUE" /> </bean>
<bean class="org.apache.cxf.jaxrs.openapi.OpenApiFeature"> <property name="usePathBasedConfig" value="TRUE" /> </bean>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow