Details
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 first time uses the classes that the Application yields.
- The second time uses the classes that the Servlet configuration describes.
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>org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet</servlet-class> <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>