7

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.

aliceangel
  • 304
  • 1
  • 5
  • 19
  • I quite don't understand the requirement of yours. But you can simply use the swagger for all your apis using the single url. like: http://localhost:8080/swagger-ui.html – Pawan Tiwari Oct 29 '21 at 08:24

1 Answers1

7

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");
            }
        };
    }
devatherock
  • 2,423
  • 1
  • 8
  • 23