1

I am developing web application with Spring boot , Jersey rest service and embedded jetty.

I crashing my head to the wall tor few days , just wanted that the request to : localhost:8082 will redirect me to index.html.

my index.html is located at /resources/static/index.html

I wrote a Spring boot controller class with @RequestMapping :

@Controller
public class WebConfig extends WebMvcConfigurerAdapter {

@RequestMapping(value = {"","/"},  method = RequestMethod.GET)
public String mainPage(HttpServletRequest request) {
    String pathInfo = request.getRequestURI();
    return "redirect:index.html;
  }


}

However , when I call : localhost:8082 it does not redirect me to index.html

Only when I call with double slashing : localhost:8082//

Can any one help me ?

My spring boot SpringBootServletInitializer class look like this :

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

}

Thanks for your answers .

lshaked
  • 109
  • 1
  • 5
  • 15
  • Possible duplicate of [redirect in Spring MVC](http://stackoverflow.com/questions/4584410/redirect-in-spring-mvc) – Magnus Sep 01 '16 at 11:04

3 Answers3

0

I found the problem.

I have jetty-rewite.xml and it is configured there :

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

So by default every call to root : localhost:8082 is redirected to welcome.html , need to chenge it to index.html

lshaked
  • 109
  • 1
  • 5
  • 15
-2

As per spring 3.0.5 try to use like this

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

Here " * " matches anything, so it will be the default handler in case no others.

Jekin Kalariya
  • 3,475
  • 2
  • 20
  • 32
  • it do the redirect to http://localhost:8082/index.html but then i get error from the page : "The localhost page isn’t working" , also all the other path's to localhost gets the same error – lshaked Sep 01 '16 at 12:50
-5

Basically in java, when you want to write "/", you must write "//". You can try this.

@RequestMapping("//")

Everything ok with me. I hope it can help you :)

Ken
  • 1