首页 > 解决方案 > 映射到应用程序根“/”失败,但除此之外正在工作

问题描述

我正在使用 Spring Boot 2.5.2。我配置了所有东西(不是安全性,它是自动配置的,需要将所有配置从 XML 转换为 Java。身份验证通过 LDAP 工作)并且工作正常,除了一件事,即映射到/-> redirect:/main

注意:这是从 Spring 迁移到带有.jspas 视图的 Spring Boot。

下面是 XML 中的映射:

<mvc:view-controller path="/" view-name="redirect:/main" />

以上在 Spring 中正常工作,但是当使用如下 Java 代码转换为 Spring Boot 时,这种特定情况不起作用:

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("redirect:/main");
    }
}

注意:如果我设置addViewController了其他类似的东西,/documentation那么它会成功重定向,但不会使用/.

标签: javaspringspring-bootspring-mvc

解决方案


@Configuration
public class WebConfiguration implements WebMvcConfigurer {

@Override
    public void addViewControllers(ViewControllerRegistry registry) {
    registry.addRedirectViewController("/", "redirect:/main");
    }
}

推荐阅读