Uploaded image for project: 'CXF'
  1. CXF
  2. CXF-8181

CXFNonSpringJaxrsServlet Feature registration fails via javax.ws.rs.Application

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 3.2.11, 3.3.4
    • Fix Version/s: 3.3.5, 3.2.12, 3.4.0
    • Component/s: JAX-RS
    • Labels:
      None
    • Estimated Complexity:
      Unknown

      Description

      CXFNonSpringJaxrsServlet always overwrites Features surfaced by a javax.ws.rs.core.Application implementation with Features specified in the servlet configuration, even if the latter set is empty.

      The server factory bean created to encapsulate the instantiated Application class has its Features list initialized twice.

      The features enumerated by the servlet config replaces the first list.
       

      I discovered the problem while trying to get OpenAPI 3.0 / Swagger-UI support to work, so FWIW, an example:

      • Application implementation that registers OpenApiFeature.

       

      package org.foo.service;
      
      import org.apache.cxf.feature.Feature;
      import org.apache.cxf.jaxrs.openapi.OpenApiFeature;
      
      import java.util.Collection;
      import java.util.HashSet;
      import java.util.Set;
      import javax.ws.rs.ApplicationPath;
      import javax.ws.rs.core.Application;
      
      import static java.util.Objects.requireNonNull;
      
      @ApplicationPath("FooService")
      public class FooServiceApplication extends Application
      {
          @Override
          public Set<Class<?>> getClasses ()
          {
              Set<Class<?>> classes = new HashSet<>();
              classes.add(Resource1.class);
              classes.add(Resource2.class); 
              return classes;
          }
      
          @Override
          public Set<Object> getSingletons ()
          {
              Set<Object> singletons = new HashSet<>();
              singletons.add(openapiFeature());
              return singletons;
          }
      
          private Feature openapiFeature () {
              /*
               * Enables automatic Swagger document generation for the web service.
               */
              OpenApiFeature feature = new OpenApiFeature();
              feature.setTitle("FooService");
              feature.setReadAllResources(false);
              feature.setPrettyPrint(true);
              feature.setSupportSwaggerUi(true);
              return feature;
          }
      }
      
      • Corresponding servlet config - Swagger-UI support does not enable unless the commented-out section below is enabled.

       

      <?xml version="1.0"?>
      <web-app xmlns="http://java.sun.com/xml/ns/javaee" version="3.0">
          
          <display-name>FooService</display-name>
          
          <servlet>
              <servlet-name>FooService</servlet-name>
              <servlet-class&gt;org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet</servlet-class&gt;
              
              <init-param>
                  <param-name>javax.ws.rs.Application</param-name>
                  <param-value>org.foo.service.FooServiceApplication</param-value>
              </init-param>
      <!--
              <init-param>
                  <param-name>jaxrs.features</param-name>
                  <param-value>org.apache.cxf.jaxrs.openapi.OpenApiFeature</param-value>
              </init-param>
      -->
      
              <load-on-startup>1</load-on-startup>
          </servlet>
          
          <servlet-mapping>
              <servlet-name>SLA835Gateway</servlet-name>
              <url-pattern>/*</url-pattern>
          </servlet-mapping>
          
          <session-config>
              <session-timeout>1</session-timeout>
          </session-config>
      </web-app>
      
      

       

       

       

        Attachments

          Activity

            People

            • Assignee:
              coheigea Colm O hEigeartaigh
              Reporter:
              noel-ang Noel Ang
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: