Multiple swagger-ui html pages in a spring application

Viewed 707

I am building an application where I would like to have two swagger urls:

http://localhost:8080/order-swagger.ui.html

http://localhost:8080/inventory-swagger.ui.html

I read many articles but did not find any solution. What I found is that to define two docket beans but it does not effectively build two html pages. It creates 2 items in the upper right corner dropdown.

Much appreciated if you could provide how to do it.

1 Answers

Along with the two Docket beans, you could add a couple of URL redirection rules to route /order-swagger.ui.html and /inventory-swagger.ui.html to /swagger-ui.html with the correct group selected. I defined a WebMvcConfigurer bean to do the redirection and then I got the two separate URLs, http://localhost:8080/order-swagger.ui.html and http://localhost:8080/inventory-swagger.ui.html available showing the API definitions of /order and /inventory endpoints respectively. Bean definitions below:

    @Bean
    public Docket orderDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("order")
                .select().paths(path -> path.endsWith("/order"))
                .build();
    }

    @Bean
    public Docket inventoryDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("inventory")
                .select().paths(path -> path.endsWith("/inventory"))
                .build();
    }

    @Bean
    public WebMvcConfigurer webMvcConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
              registry.addRedirectViewController("order-swagger.ui.html", "/swagger-ui.html?urls.primaryName=order");
              registry.addRedirectViewController("inventory-swagger.ui.html", "/swagger-ui.html?urls.primaryName=inventory");
            }
        };
    }
Related