java - Spring Boot 请求映射根路径 Controller 仅使用双斜杠

标签 java spring-mvc request-mapping

我正在使用 Spring boot、Jersey Rest 服务和嵌入式 jetty 开发 Web 应用程序。

几天来我把头撞到墙上,只是希望对 localhost:8082 的请求将我重定向到index.html。

我的index.html位于/resources/static/index.html

我用 @RequestMapping 编写了一个 Spring boot Controller 类:

@Controller
public class WebConfig extends WebMvcConfigurerAdapter {

@RequestMapping(value = {"","/"},  method = RequestMethod.GET)
public String mainPage(HttpServletRequest request) {
    String pathInfo = request.getRequestURI();
    return "redirect:index.html;
  }


}

但是,当我调用:localhost:8082时,它不会将我重定向到index.html

仅当我使用双斜线调用时:localhost:8082//

有人可以帮助我吗?

我的 spring boot SpringBootServletInitializer 类如下所示:

@Configuration
@EnableAutoConfiguration
@ComponentScan(basePackages = { "com.wfm.api"})
public class Launcher extends SpringBootServletInitializer {

private static ApplicationContext applicationContext = null;


public static void main(String[] args) throws Exception {
     String mode = args != null && args.length > 0 ? args[0] : null;

        // argument parameters 'stop' that comes from class WindowsServiceLauncher which in lance when starting windows service using procrun
        if (applicationContext != null && mode != null && "stop".equals(mode)) {
            System.exit(SpringApplication.exit(applicationContext, new ExitCodeGenerator() {
                @Override
                public int getExitCode() {
                    return 0;
                }
            }));
        }
        else {
            SpringApplication app = new SpringApplication(Launcher.class);
            applicationContext = app.run(args);   
        }
}

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(Launcher.class);
}

/**
 * Registrating REST Servlet
 */
@Bean
public ServletRegistrationBean jersyServletRegistration() {
    ServletRegistrationBean registration = new ServletRegistrationBean(new ServletContainer(),"/pethome/api/rest/*");
    Map<String,String> params = new HashMap<String,String>();
    params.put(ServletProperties.JAXRS_APPLICATION_CLASS, JersyRestConfigurer.class.getName());
    //params.put(ServerProperties.WADL_GENERATOR_CONFIG, WadlGeneratorConfigurer.class.getName());

    registration.setInitParameters(params);
    return registration;
}


/**
 * Define Spring boot Server container , We use Jetty
 */
@Bean
public EmbeddedServletContainerFactory containerFactory() {
    final JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory = new JettyEmbeddedServletContainerFactory() {
        @Override
        protected JettyEmbeddedServletContainer getJettyEmbeddedServletContainer(Server server) {
            return new JettyEmbeddedServletContainer(server);
        }
    };
    jettyEmbeddedServletContainerFactory.addServerCustomizers(new JettyConfigurer());
    return jettyEmbeddedServletContainerFactory;
}


@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    WebApplicationContext rootAppContext = createRootApplicationContext(servletContext);
    if (rootAppContext != null) {
        //servletContext.addListener(new MailSenderLoadingListener());
    }
    else {
        this.logger.debug("No ContextLoaderListener registered, as "
                + "createRootApplicationContext() did not "
                + "return an application context");
    }
}

}

感谢您的回答。

最佳答案

我发现了问题。

我有 jetty-rewrite.xml 并在那里配置:

  <Call name="addRule">
            <Arg>
                <New class="org.eclipse.jetty.rewrite.handler.RewriteRegexRule">
                    <Set name="regex">/</Set>
                    <Set name="replacement">/welcome.html</Set>
                </New>
            </Arg>
        </Call>

因此默认情况下,每次对 root 的调用: localhost:8082 都会重定向到welcome.html,需要将其更改为index.html

关于java - Spring Boot 请求映射根路径 Controller 仅使用双斜杠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39268827/

相关文章:

Java:部分应用泛型类 - 如何消除冗余类型参数?

java - 当屏幕方向改变时,TabLayout 会卡在 viewpager 上

java - 如何使用@RestController登录并将对象发送到angularjs

java - 无法找到逻辑名称为 @embeddedid 的列

java - Spring MVC @RequestMapping 参数问题

java - 如何设置 Spring-Boot 请求映射方法的优先级

java - 为什么不能在 Java 中创建通用填充方法?

java - 提交期间的延迟(使用 JPA/JTA)

maven - 在 Spring Junit 测试用例中获取 jdbcTemplate null

java - 如何将 127.0.0.1 重定向到 localhost (Java)?