swaggerswagger-uiwildfly-11

How to define basePath in Swagger 3.0 inside Wildfly


I integrated Swagger-ui in my wildfly application.

The project is configured through maven with the following (relevant) dependencies:

    <dependency>
      <groupId>io.swagger.core.v3</groupId>
      <artifactId>swagger-jaxrs2</artifactId>
      <version>2.0.0</version>
    </dependency>
    <dependency>
      <groupId>io.swagger.core.v3</groupId>
      <artifactId>swagger-jaxrs2-servlet-initializer</artifactId>
      <version>2.0.0</version>
    </dependency>
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>swagger-ui</artifactId>
        <version>3.23.2</version>
    </dependency>

I also extracted the swagger-ui.html page from the webjar and customized the javascript to connect to my server as follow (see the url):

window.onload = function() {
  // Begin Swagger UI call region
  const ui = SwaggerUIBundle({
    url: "doc/openapi.json",
    dom_id: "#swagger-ui",
    deepLinking: true,
    presets: [
      SwaggerUIBundle.presets.apis,
      SwaggerUIStandalonePreset
    ],
    plugins: [
      SwaggerUIBundle.plugins.DownloadUrl
    ],
    layout: "StandaloneLayout"
  })
  // End Swagger UI call region

  window.ui = ui
}

The swagger-ui is working well, it displays the list of my endpoints. But when I try to connect to my endpoints, it fails because the url used to connect does not contain the context path of my application (myApp here), it tries to connect with:

curl -X GET "http://localhost:8080/aop/hello/version" -H "accept: */*"

But it should be:

curl -X GET "http://localhost:8080/myApp/aop/hello/version" -H "accept: */*"

I found that the missing part is named the basePath but I did not find any solution to add it in my Wildfly application.

Update: Following the documentation, it seems that the basePath comes from Swagger, the OpenApi uses a server. Then I tried:

@ApplicationPath("/api")
public class ApplicationConfig extends Application 
{
    public ApplicationConfig(@Context ServletConfig servletConfig) {
        // Swagger Configuration
        super();
        OpenAPI oas = new OpenAPI()
                .servers(Collections.singletonList(new Server().url("http://localhost:8080/my_app")));
        Info info = new Info()
                .title("Swagger");

        oas.info(info);
        SwaggerConfiguration oasConfig = new SwaggerConfiguration()
                .openAPI(oas)
                .prettyPrint(true);

        try {
            new JaxrsOpenApiContextBuilder<>()
                    .servletConfig(servletConfig)
                    .application(this)
                    .openApiConfiguration(oasConfig)
                    .buildContext(true);
        } catch (OpenApiConfigurationException e) {
            throw new ConfigurationException(e.getMessage(), e);
        }
    }
}

But it is still not taken into account.

Any lead?

Thanks :-)


Solution

  • Finally, I got it working, but not with the Java configuration which is not taken into account (seen in debug) but using a configuration file openapi.yaml in the classpath of the application.

    The file looks like:

    prettyPrint: true
    cacheTTL: 0
    openAPI:
      servers:
        - url: /my_app
      info:
        version: '1.0'
        title: Swagger application
    

    With the server url now correctly handled.

    But I am still curious to know what should be the right solution for a Java configuration.