首页 > 解决方案 > Spring Boot 2.1.3.RELEASE,我想要 swagger-ui.html 作为主页/欢迎页面并处理 ROOT 上下文 (/)

问题描述

我在http://localhost:8448/portalapi/swagger-ui.html有 spring boot 2 和 swagger API 页面。但我想映射http://localhost:8448/http://localhost:8448/portalapi/将请求转发到http://localhost:8448/portalapi/swagger-ui.html。但它不起作用。它给出 HTTP 状态 404 - 未找到(tomcat 默认响应)。非常感谢任何帮助或指针。

应用程序属性

##
# tomcat server port
server.port=8448
#server.address=0.0.0.0

server.servlet.contextPath=/portalapi/

这是一个用于转发上下文的 webconfig 类:

   @Configurable
    @Component
    public class WebConfig implements WebMvcConfigurer {

        @Override
        public void addViewControllers(ViewControllerRegistry registry) {
            // forward requests to /admin and /user to their index.html
            registry.addViewController("/").setViewName(
                    "forward:/portalapi/swagger-ui.html");
            registry.addViewController("/portalapi/").setViewName(
                    "forward:/portalapi/swagger-ui.html");
        }

    }

标签: springspring-bootswaggerswagger-uiembedded-tomcat-8

解决方案


将下面的部分从:

@Configurable
@Component
public class WebConfig implements WebMvcConfigurer {

至:

@Configuration
public class WebConfig implements WebMvcConfigurer {

推荐阅读