【发布时间】:2020-11-11 14:40:17
【问题描述】:
我有一个 Spring Boot 项目,需要从
更新我的招摇路径http://localhost:9012/swagger-ui.html to http://localhost:9012/myservice/swagger-ui.html
我没有为我的应用程序使用任何上下文路径我已经尝试了以下所有方法,但都没有奏效。
Change location to call swagger-ui in Spring
https://github.com/springfox/springfox/issues/1443
How to change swagger-ui.html default path
https://github.com/springfox/springfox/issues/1080
How to change Swagger-ui URL prefix?
private static final String PATH = "/myservice";
@Override
public void addViewControllers(ViewControllerRegistry registry) {
final String apiDocs = "/v2/api-docs";
final String configUi = "/swagger-resources/configuration/ui";
final String configSecurity = "/swagger-resources/configuration/security";
final String resources = "/swagger-resources";
final String swaggerUI = "/swagger-ui.html";
registry.addRedirectViewController(apiDocs, PATH +apiDocs).setKeepQueryParams(true);
registry.addRedirectViewController(resources, PATH +resources);
registry.addRedirectViewController(configUi, PATH +configUi);
registry.addRedirectViewController(configSecurity, PATH +configSecurity);
registry.addRedirectViewController(swaggerUI, PATH + swaggerUI);
registry.addRedirectViewController("/",PATH);
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry){
registry.addResourceHandler(PATH + "/swagger-ui.html**").addResourceLocations("classpath:/META-INF/resources/swagger-ui.html");
registry.addResourceHandler(PATH + "/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
我正在使用 swagger 版本 2.9.2 有没有其他方法可以解决这个问题。
【问题讨论】:
-
嗨@Kashyap,你能检查一下howtodoinjava.com/swagger2/swagger-spring-mvc-rest-example
-
尝试在您的 application.properties 中添加上下文路径
server.servlet.contextPath=/myservice看看是否有效。 -
嗨 LUC1F3R 和 Sudoss,如果我向我的应用程序添加上下文路径,这两种方式都建议添加上下文路径,所有其他 API URL 也会发生我不想要的更改。
-
@Kashyap 知道了...看起来重定向/转发就是您要找的东西。请尝试使用代码 sn-p here 并告诉我们这是否适合您。
标签: java spring-boot swagger swagger-ui swagger-2.0