3
votes

I need to configure 2 servlets: one for regular http requests and the other one is the Atmosphere servlet for Java web-socket.

Here is the code of my WebApplicationInitializer:

public class AppInitializer implements WebApplicationInitializer
{
    private static final String CONFIG_LOCATION = "com.mysite.myapp.presentation.config";
    private static final String MAPPING_URL = "/*";
    private static final String STREAM_URL = "/stream/*";

    private int servletInx = 1;
    private ServletContext servletContext;

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException
    {
        this.servletContext = servletContext;
        WebApplicationContext context = getContext();
        servletContext.addListener(new ContextLoaderListener(context));
        registerServlet("DispatcherServlet", new DispatcherServlet(context), MAPPING_URL);
        registerServlet("AtmosphereServlet", new AtmosphereServlet(), STREAM_URL);
    }

    private AnnotationConfigWebApplicationContext getContext()
    {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.setConfigLocation(CONFIG_LOCATION);
        return context;
    }

    private void registerServlet(String servletName, Servlet servletClass, String mappingUrl)
    {
        ServletRegistration.Dynamic dispatcher =
            servletContext.addServlet(servletName, servletClass);

        if (dispatcher != null)
        {
            System.out.println("servletInx: " + servletInx);
            dispatcher.setLoadOnStartup(servletInx++);
            dispatcher.addMapping(mappingUrl);
        }
    }
}

When running the application, the http part works fine; however, no static files are being served. Even the index.html at webapps/myapp (localhost:8080/myapp/index.html) is returning a 404

When a controller returns the same html via a /@RequestMapping(value = "/welcome", method = RequestMethod.GET), it works but any javascript or css specified in the html returns with a 404

Any help will greatly appreciated

1

1 Answers

0
votes

Sorry, it had nothing to do with the WebApplicationInitializer. It had to do with WebMvcConfigurerAdapter. It was missing the configureDefaultServletHandling method enabling the configurer. This in turn enables the application to serve static content.

Sorry about the scratching of the heads ;-)