Versions Compared

Key

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

...

These features can be configured programatically in Java or using Spring or Blueprint beans.

Setup

Code Block
languagexml
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-rs-service-description-swagger</artifactId>
    <version>3.1.7</version>
</dependency>

Note that a cxf-rt-rs-service-description needs to be used if older CXF 3.1.x versions are used.

 

Properties

The following optional parameters can be configured in Swagger2Feature

...

Code Block
xml
xml
<web-app>
    <context-param>
        <param-name>contextParam</param-name>
        <param-value>contextParamValue</param-value>
    </context-param>
    <servlet>
        <servlet-name>CXFServlet</servlet-name>
        <display-name>CXF Servlet</display-name>
        <servlet-class>
              org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet
        </servlet-class>
        <init-param>
            <param-name>jaxrs.serviceClasses</param-name>
            <param-value>
                           org.apache.cxf.systest.jaxrs.BookStore
            </param-value>
        </init-param>
        <init-param>
            <param-name>jaxrs.features</param-name>
            <param-value>
                        org.apache.cxf.jaxrs.swagger.Swagger2Feature
                        (basePath=/somepath)
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
    
</web-app>

Accessing Swagger

...

Documents

When Swagger is enabled by Swagger feature, the Swagger documents will be available at the location URL constructed of the service endpoint location followed by /swagger.json or /swagger.yaml.

For example, if the service lets assume a JAX-RS endpoint is published at httpat 'http://host:port/serviceroot, its context/services/' where 'context' is a web application context,  "/services" is a servlet URL. In this case its Swagger documents are available at 'http://host:port/context/servicerootservices/swagger.json and httpjson' and 'http://host:port/context/servicerootservices/swagger.yaml'.

Starting from CXF 3.1.7 the CXF Services page will link to Swagger documents if Swagger2Feature is active. 

In the above example, go to 'http://host:port/context/services/services' and follow a Swagger link which will return a Swagger JSON document.If you deploy swagger-ui with your application, you can access these Swagger documents directly.  For more details, please look at some of the samples listed below.

Enabling Swagger UI

First one needs to add the following

Code Block
languagexml
<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>swagger-ui</artifactId>
    <version>2.1.8-M1</version>
</dependency>

Automatic UI Activation

This feature is available starting from CXF 3.1.7: Adding a Swagger UI Maven dependency is all what is needed to start accessing Swagger documents with the help of Swagger UI.

For example, lets assume a JAX-RS endpoint is published at 'http://host:port/context/services/'.

Open the browser and go to 'http://host:port/context/services/api-docs?/url=/swagger.json' which will return a Swagger UI page.

CXF Services page will also link to Swagger UI. Go to 'http://host:port/context/services/services' and follow a Swagger link which will return a Swagger UI page.

See samples/jax_rs/description_swagger2samples/jax_rs/spring_boot and samples/jax_rs/spring_boot_scan 

Unpacking Swagger UI resources

Up until CXF 3.1.7 unpacking Swagger UI resources into the local folder has been the only option. It is demoed in all CXF Swagger demos but samples/jax_rs/description_swagger2 which

shows the automatic UI activation. In CXF 3.1.7: set Swagger2Feature 'supportSwaggerUi' property to 'false' to disable to automatic UI activation described in the previous section

Samples

CXF's distribution contains the following samples.

...