dofkee
dofkee

Reputation: 11

Swagger integration for multiple jaxrs servers

Is it possible to configure swagger for multiple jaxrs servers (multiple subsystems) using one Swagger config with common basepath? For example:

Servlet configuration:

<init-param>
    <param-name>swagger.api.basepath</param-name>
    <param-value>http://localhost:9000/services</param-value>
</init-param>

Context.xml configuration

Server 1:

<jaxrs:server id="sampleServer" address="/swaggerSample/v1">
        <jaxrs:serviceBeans>
            <ref bean="sampleResource1" />
        </jaxrs:serviceBeans>
        <jaxrs:providers>
            <ref bean="jsonProvider" />
            <ref bean="multipartProvider" />
        </jaxrs:providers>
        <jaxrs:features>
            <ref bean="swagger2Feature" />
        </jaxrs:features>
</jaxrs:server>

Server 2:

<jaxrs:server id="sampleServer" address="/swaggerSample/v2">
        <jaxrs:serviceBeans>
            <ref bean="sampleResource2" />
        </jaxrs:serviceBeans>
        <jaxrs:providers>
            <ref bean="jsonProvider" />
            <ref bean="multipartProvider" />
        </jaxrs:providers>
        <jaxrs:features>
            <ref bean="swagger2Feature" />
        </jaxrs:features>
</jaxrs:server>

Server 1:

Expected result: http://localhost:9000/services/swaggerSample/v1/sample/{name}

Actual result: http://localhost:9000/services/sample/{name}

Server 2: Server 2 takes old basepath from server 1:

Expected result: http://localhost:9000/services/swaggerSample/v2/sample/{name}

Actual result: http://localhost:9000/services/sample/{name}

Upvotes: 1

Views: 1490

Answers (3)

halil ibrahim binol
halil ibrahim binol

Reputation: 194

Set usePathBasedConfig = true :

swagger2Feature.setUsePathBasedConfig(true);       

Upvotes: 1

loicmathieu
loicmathieu

Reputation: 5562

I came across the same issue (using CXF 3.1.11 or 3.2.1) and found a workaround that I will share.

First, according to me, there is a bug in the CxfSwagger2Features : you can define multiple beans or one bean it didn't change, if you have multiple cxf jaxrs:server all ressources of all servers will be shown in the swagger file of each service. And if the base path of your service is the same it will collide.

Basically, CxfSwagger2Features didn't care that you use a different address in the XML definition, it use the @Path of the Service to identify the ressources (so the @Path of your bean) and if you have the same @Path value in both your beans (sampleResource1 and sampleResource2 here) it will collide.

The workaround is to have for each server different addresses (like you have) and each bean (sampleResource1 and sampleResource2) a different @Path. This will messed up a little your URL but this will works.

I will try to find the root of the issue if I have time and suggest a fix for it but for the moment I can only think of the exposed workaround.

Regards,

Loïc

Upvotes: 0

Grzegorz Grzybek
Grzegorz Grzybek

Reputation: 6237

Is swagger2Feature a reference to org.apache.cxf.jaxrs.swagger.SwaggerFeature? if so, then I'm afraid you can't share this reference because it contains state that can't be shared...

Upvotes: 0

Related Questions