我正在使用 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/