【问题标题】:Spring Boot request mapping Controller for root path working only with double slash根路径的 Spring Boot 请求映射控制器仅使用双斜杠
【发布时间】:2016-09-01 10:16:23
【问题描述】:

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

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

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

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

@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");
    }
}

}

感谢您的回答。

【问题讨论】:

标签: java spring-mvc request-mapping


【解决方案1】:

我发现了问题。

我有 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

【讨论】:

    【解决方案2】:

    根据 spring 3.0.5 尝试像这样使用

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

    这里的“*”匹配任何东西,所以它将是默认处理程序,以防没有其他处理程序。

    【讨论】:

    • 它重定向到localhost:8082/index.html 但随后我从页面收到错误:“本地主机页面不工作”,所有其他到本地主机的路径也得到相同的错误
    【解决方案3】:

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

    @RequestMapping("//")
    

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

    【讨论】:

    • 相信你想到的是反斜杠();正斜杠 (/) 可以用 Java 简单地编写。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-25
    • 2012-05-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多