集成多个Swagger2Feature实例

时间:2018-01-17 15:21:09

标签: java xml cxf swagger

我正在尝试创建2个Swagger unser 2不同上下文的实例:

http://localhost:8080/app/rest1/swagger.json

http://localhost:8080/app/rest2/swagger.json

所以我有第一个正常工作的实例(http://localhost:8080/app/rest1/swagger.json):

context1.xml

<context:component-scan base-package="org.app.rest.cxf.service"/>  

<bean id="swagger2customizer" class="org.apache.cxf.jaxrs.swagger.Swagger2Customizer">
    <property name="dynamicBasePath" value="true"/>
    <property name="replaceTags" value="true"/>
</bean>
<bean id="swagger2Feature" class="org.apache.cxf.jaxrs.swagger.Swagger2Feature">
    <property name="title" value="Test"/>
    <property name="version" value="1.0"/>
    <property name="description" value="Test 1.0"/>    
    <property name="contact" value="dev@syncope.apache.org"/>    

    <property name="resourcePackage" value="org.app.rest.api.service"/>
    <property name="scanAllResources" value="true"/>
    <property name="activateOnlyIfJaxrsSupported" value="true"/>

    <property name="customizer" ref="swagger2customizer"/>
</bean>

<jaxrs:server id="container" address="/"
                basePackages="org.app.rest.api.service, org.app.rest.cxf.service" 
                staticSubresourceResolution="true">
    <jaxrs:properties>
      <entry key="search.lax.property.match" value="true"/>
      <entry key="convert.wadl.resources.to.dom" value="false"/>
    </jaxrs:properties> 
    <jaxrs:inInterceptors>
      <ref bean="gzipInInterceptor"/>
      <ref bean="validationInInterceptor"/>
    </jaxrs:inInterceptors>         
    <jaxrs:outInterceptors>
      <ref bean="gzipOutInterceptor"/>
      <ref bean="validationOutInterceptor"/>
    </jaxrs:outInterceptors>
    <jaxrs:providers>
      <ref bean="dateParamConverterProvider"/>
      <ref bean="jaxbProvider"/>
      <ref bean="jsonProvider"/>
      <ref bean="exceptionMapper"/>
      <ref bean="searchContextProvider"/>
      <ref bean="addDomainFilter"/>
      <ref bean="addETagFilter"/>
      <ref bean="wadlGenerator"/>
    </jaxrs:providers>
    <jaxrs:features>
      <ref bean="swagger2Feature"/>
    </jaxrs:features>
</jaxrs:server>

所以访问http://localhost:8080/app/rest1/swagger.json会生成包含所有端点的完整json。 我尝试配置第二个Swagger,它在不同的包下引用其他服务:

context2.xml

<context:component-scan base-package="com.app2.rest.cxf.service"/>  

<bean id="swagger2customizer2" class="org.apache.cxf.jaxrs.swagger.Swagger2Customizer">
    <property name="dynamicBasePath" value="true"/>
    <property name="replaceTags" value="true"/>
</bean>
<bean id="swagger2Feature2" class="org.apache.cxf.jaxrs.swagger.Swagger2Feature">
    <property name="title" value="Test"/>
    <property name="version" value="1.0"/>
    <property name="description" value="Test 1.0"/>    
    <property name="contact" value="dev@syncope.apache.org"/>    

    <property name="resourcePackage" value="com.app2.rest.api.service"/>
    <property name="scanAllResources" value="true"/>
    <property name="activateOnlyIfJaxrsSupported" value="true"/>

    <property name="customizer" ref="swagger2customizer2"/>
</bean>

<jaxrs:server id="container" address="/"
                basePackages="com.app2.rest.api.service, com.app2.rest.cxf.service" 
                staticSubresourceResolution="true">
    <jaxrs:properties>
      <entry key="search.lax.property.match" value="true"/>
      <entry key="convert.wadl.resources.to.dom" value="false"/>
    </jaxrs:properties> 
    <jaxrs:inInterceptors>
      <ref bean="gzipInInterceptor"/>
      <ref bean="validationInInterceptor"/>
    </jaxrs:inInterceptors>         
    <jaxrs:outInterceptors>
      <ref bean="gzipOutInterceptor"/>
      <ref bean="validationOutInterceptor"/>
    </jaxrs:outInterceptors>
    <jaxrs:providers>
      <ref bean="dateParamConverterProvider"/>
      <ref bean="jaxbProvider"/>
      <ref bean="jsonProvider"/>
      <ref bean="exceptionMapper"/>
      <ref bean="searchContextProvider"/>
      <ref bean="addDomainFilter"/>
      <ref bean="addETagFilter"/>
      <ref bean="wadlGenerator"/>
    </jaxrs:providers>
    <jaxrs:features>
      <ref bean="swagger2Feature2"/>
    </jaxrs:features>
</jaxrs:server>

的web.xml

<servlet>
    <servlet-name>AppSwaggerCXFServlet</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    <init-param>
        <param-name>config-location</param-name>
        <param-value>classpath*:/context2.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup> 
</servlet>
<servlet-mapping>
    <servlet-name>AppSwaggerCXFServlet</servlet-name>
    <url-pattern>/sih/*</url-pattern>
</servlet-mapping>

问题是,第二个网址http://localhost:8080/app/rest2/swagger.json会在第一个Swagger配置的包下生成一个包含所有端点的JSON(因此在org.app.rest.api.service下而不是com.app2.rest.api.service)。

您可以帮助我获得正确的配置吗?

由于

1 个答案:

答案 0 :(得分:0)

TL; DR:

添加你的swagger2feature bean config:

<property name="usePathBasedConfig" value="true"/>

或者,如果您在Configuration bean中创建了Swogramger2Feature:

feature.setUsePathBasedConfig(Boolean.TRUE);

为什么:

Swagger inspect模型基于 io.swagger.config.SwaggerConfig ,提供Swagger生成的所有入口点规范和其他参数。 SwaggerConfig 在映射到&#34;配置密钥&#34; io.swagger.jaxrs.config.SwaggerConfigLocator 中进行管理,并且从 io.swagger.jaxrs.config.SwaggerContextService 创建。

配置键的默认值为&#34; swagger.config.id.default&#34; 仅当 usePathBasedConfig <时才添加API basePath SwaggerContextService 的/ em>属性设置为 true

来自CXF的 Swagger2Feature 允许指定 SwaggerContextService 的属性,您已理解它:&#34; usePathBasedConfig&#34; <的默认值/ em>为false,表示所有 SwaggerConfig 都使用相同的键&#34; swagger.config.id.default&#34放入 SwaggerConfigLocator ; ,意思是他们互相压倒。

Swagger2Feature 中启用此功能后,配置将独立管理,并且每个Swagger生成过程都可以很好地访问。

我真的不知道默认情况下没有启用它的原因,因为只使用一个启用的入口点来管理它就不是问题。