Swagger-UI and dynamic host targets

502 views
Skip to first unread message

Al Spohn

unread,
Mar 9, 2018, 3:07:46 PM3/9/18
to Swagger
I'm using JAX-RS with Swagger via a servlet implementation that pretty much follows the "Petstore" tutorial.  My swagger.json document is being generated fine, but although I can reference it via the URL, I can't find it in my project directory to save my life - so I obviously have some form of pretty fundmental misunderstanding going on there.  But my main question is how  can accommodate running swagger-ui in a way that it is environmentally sensitive (as in dev/int/prod.)  I am already getting Tomcat environmental variables and using them elsewhere in my app for stuff like db connections, but I don't know where to make that happen in the swagger-UI context.  I tried grabbing those environmental variables in my servlet (that sets up the swagger configuration) to change the host via beanConfig.setHost() but that seems to have no effect.  I'm hoping I can get this done without having to write the actual index.html file that calls the UI.  Here's what my stuff looks like for the most part:

Here's the servlet with my failed attempt to pass environmental awareness:

public class SwaggerConfiguration extends HttpServlet{

    public void init(ServletConfig config) throws ServletException {
        super.init(config);

        Service swaggerInfo = new Service();

        String swaggerHost = swaggerInfo.getSwaggerHost();

        System.out.println("Swagger Host: " + swaggerHost);

        BeanConfig beanConfig = new BeanConfig();
        beanConfig.setTitle("Committee Manager");
        beanConfig.setVersion("1.0");
        beanConfig.setSchemes(new String[]{"http"});
        //beanConfig.setHost("localhost:8080");
        beanConfig.setHost(swaggerHost);
        beanConfig.setBasePath("/rest");
        beanConfig.setResourcePackage("edu.mayo.dbconnection");
        beanConfig.setScan(true);
        beanConfig.setDescription("Committee Manager API");
    }
}

web.xml

    ...
    <servlet>
        <servlet-name>SwaggerBootstrap</servlet-name>
        <servlet-class>edu.mayo.swagger.SwaggerConfiguration</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>

index.html

...
window.onload = function() {
  
  // Build a system
  const ui = SwaggerUIBundle({
      url: "http://localhost:8080/rest/swagger.json", //I'm hoping I don't have to replace this dynamically somehow...
    dom_id: '#swagger-ui',
    deepLinking: true,
    presets: [
      SwaggerUIBundle.presets.apis,
      SwaggerUIStandalonePreset
    ],
    plugins: [
      SwaggerUIBundle.plugins.DownloadUrl
    ],
    layout: "StandaloneLayout"
  })

  window.ui = ui
}
...

Thanks very much in advance for any nudges in the right direction....

Reply all
Reply to author
Forward
0 new messages