Spring Boot请求映射根路径的控制器仅使用双斜杠

时间:2016-09-01 10:16:23

标签: java spring-mvc request-mapping

我正在使用Spring启动,Jersey休息服务和嵌入式码头开发Web应用程序。

我几天都撞到了墙上,只是想请求:localhost:8082会将我重定向到index.html。

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

我用@RequestMapping写了一个Spring启动控制器类:

@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 //

任何人都可以帮助我吗?

我的春季启动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");
    }
}

}

感谢您的回答。

3 个答案:

答案 0 :(得分:0)

我发现了问题。

我有jetty-rewite.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

答案 1 :(得分:-1)

按照春季3.0.5尝试使用这样的

@RequestMapping(value={"/", " * "})

这里&#34; *&#34;匹配任何东西,所以它将是默认的处理程序,如果没有其他人。

答案 2 :(得分:-3)

基本上在java中,当你想写“/”时,你必须写“//”。你可以试试这个。

@RequestMapping("//")

一切都和我好。我希望它可以帮助你:))