是否可以将Swagger 2内容移动到另一个基本URL?
我已经看到“ how to do friendly base url for swagger 2.8.0”以及这种解决方法:
@RequestMapping("/swagger")
public String greeting() {
return "redirect:/swagger-ui.html";
}
问题:
在我看来,这是不可能的!
我的服务器托管两个Web应用程序和一个REST api,这就是为什么我在这里遇到这个怪物的原因:
@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";
}
}
此设置与Spring Security设置一起使用。
我需要大摇大摆才能在/a/swagger-ui.html
下工作,但这有可能吗?
在我的ForwardController
下,以下各项均无法工作:
答案 0 :(得分:0)
您可以定义RouterFunction
@SpringBootApplication public class MyApp { public static void main(String[] args) { SpringApplication.run(MyApp.class, args); } @Bean RouterFunction<ServerResponse> routerFunction() { return route(GET("/swagger"), req -> ServerResponse.temporaryRedirect(URI.create("swagger-ui.html")).build()); } }
或创建一个WebFilter:
@Component public class SwaggerWebFilter implements WebFilter { @Override public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) { ServerHttpRequest request = exchange.getRequest(); String path = request.getPath().pathWithinApplication().value(); if (path.startsWith("/api/swagger-ui.html") || path.startsWith("/api/webjars") || path.startsWith("/api/api-docs") || path.startsWith("/api/configuration") || path.startsWith("/api/swagger-resources") || path.startsWith("/api/v2"))) { exchange = exchange.mutate().request(request.mutate().path(path.substring(4)).build()).build(); } return chain.filter(exchange); } }