![](/img/trans.png)
[英]Pagination in Spring Boot via path variable - Requested path variable is not present in Controller request mapping annotations
[英]Spring Boot request mapping Controller for root path working only with double slash
我正在使用Spring Boot,Jersey Rest Service和嵌入式Jetty開發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 //
誰能幫我 ?
我的春季啟動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-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。
從春季3.0.5開始,嘗試像這樣使用
@RequestMapping(value={"/", " * "})
這里的“ *”匹配任何內容,因此如果沒有其他內容,它將是默認處理程序。
基本上在Java中,當您要編寫“ /”時,必須編寫“ //”。 你可以試試看
@RequestMapping("//")
一切都好。 希望它能對您有所幫助:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.