【发布时间】: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