6

Is there any way to move the Swagger 2 content to another base URL?

I have seen "how to do friendly base url for swagger 2.8.0" as well as this kind of workaround:

@RequestMapping("/swagger")
public String greeting() {
    return "redirect:/swagger-ui.html";
}

The problem:

This is not possible in my case!

My server hosts two web applications and a REST api which is why I have this monster here:

@Controller
public class ForwardController {

    @RequestMapping(value = "/sitemap.xml", method = RequestMethod.GET)
    public String redirectSitemapXml(HttpServletRequest request) {
        return "forward:/a/web/assets/sitemap.xml";
    }

    @RequestMapping(value = "/robots.txt", method = RequestMethod.GET)
    public String redirectRobotTxt(HttpServletRequest request) {
        return "forward:/a/web/assets/robots.txt";
    }

    @RequestMapping(value = "/*", method = RequestMethod.GET)
    public String redirectRoot(HttpServletRequest request) {
        return "forward:/a/web/index.html";
    }

    @RequestMapping(value = "/a/**/{path:[^.]*}", method = RequestMethod.GET)
    public String redirectClients(HttpServletRequest request) {

        String requestURI = request.getRequestURI();

        if (requestURI.startsWith("/a/admin/")) {
            return "forward:/a/admin/index.html";
        }

        return "forward:/a/web/index.html";
    }

}

This goes along with Spring Security settings for this setup.

I need swagger to be available under /a/swagger-ui.html, but is this possible?


The following cannot work either under my ForwardController:

Julian
  • 33,915
  • 22
  • 119
  • 174
Stefan Falk
  • 23,898
  • 50
  • 191
  • 378
  • Did you tried other solutions in link as `SwaggerWebFilter` or `RouterFunction`? – Ori Marko Jun 16 '19 at 14:33
  • @user7294900 I cannot find those terms. Could you elaborate? – Stefan Falk Jun 19 '19 at 09:48
  • Are you using Spring MVC or WebFlux? – John Zhang Jun 19 '19 at 09:54
  • just wondering: every _forward_ starts with _/a/..._ and you want to access swagger via _/a/swagger-ui.html_ Why not setup your context-path to _/a_ ? ```server.servlet.context-path=/a``` – Dirk Deyne Jun 20 '19 at 19:01
  • @DirkDeyne Wouldn't hat move *all* my endpoints to `/a`? There is also a REST API which I serve under `/r/api`. – Stefan Falk Jun 21 '19 at 09:36
  • @displayname yes it would, so that won't work. Assumed, based on your controller there are only /a/... endpoints. – Dirk Deyne Jun 21 '19 at 15:46
  • Does this answer to your question: https://github.com/OAI/OpenAPI-Specification/issues/408 ? `Multiple basPaths is not allowed in Swagger. You can use / as your basePath (or don't specify it) (...)` – Sara Jun 06 '20 at 13:48

0 Answers0