如何在spring boot中在浏览器中查看默认url?

Sam*_*bit 2 java spring spring-mvc swagger spring-boot

我有一个使用Spring Boot 2.1.3版本开发的微服务,并且我还使用 SpringFox 2.9.2 版作为 Swagger 文档。每次当我向第三方或任何其他人分发或部署时,我都必须始终提及 swagger url,以便用户通过 REST 端点。我的问题是如何在 spring 启动的情况下创建默认重定向 url,以便它应该自动重定向到 swagger-ui.html。这意味着如果用户在浏览器中输入http://localhost:8080,浏览器应该自动重定向到 url ie。http://localhost:8080/api/swagger-ui.html。我想知道这是否需要任何配置?

在使用 stackoverflow 之前,我浏览了以下链接并尝试过,但没有任何效果。

Java Spring Boot:如何将我的应用程序根目录(“/”)映射到 index.html?

更改作为战争部署的 spring-boot 应用程序的默认欢迎页面

我也尝试了不同的方法,但我总是得到 404 或白标错误页面。我想知道有什么方法可以防止白标错误页面应该自动重定向到 swagger 页面,即。http://localhost:8080/api/swagger-ui.html

我还在 application.properties 中添加了以下内容。

server.servlet.context-path=/api
Run Code Online (Sandbox Code Playgroud)

请在这方面帮助我。

小智 5

你可以像这样添加一个 RedirectViewController:

@Configuration
public class WebConfiguration implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addRedirectViewController("/", "/api/swagger-ui.html");
    }

}
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

7876 次

最近记录:

6 年,9 月 前