0

I'm starting a new spring mvc project and I want to do all the configuration with annotation. I think that the problem that I have is migrating the web.xml to the AppInitializer, because when I made the changes and deploy the app with the new config, I get 404 for a url that was working fine until the change

my current web.xml is:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">

    <servlet>
        <servlet-name>sample</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextClass</param-name>
            <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
        </init-param>

        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>ar.com.orangebit.springMvcQuickstart.config</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>sample</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

And the AppInitializer that I think that have something wrong is:

public class AppInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext)
            throws ServletException {
        WebApplicationContext context = getContext();
        servletContext.addListener(new ContextLoaderListener(context));
        ServletRegistration.Dynamic dispatcher = servletContext.addServlet(
                "DispatcherServlet", new DispatcherServlet(context));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/*");
    }

    private AnnotationConfigWebApplicationContext getContext() {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.setConfigLocation("ar.com.orangebit.springMvcQuickstart.config");
        return context;
    }

}

With the class above, when I made a gradle jettyRunWar and enter localhost:8080 I see the directories META-INF and WEB-INF instead of the home page of the webapp.

The currently code it's on github if you want to see the rest of the project

Rumal
  • 1,452
  • 1
  • 17
  • 31

1 Answers1

0

The issue only comes up when you use the jetty plugin. If you run the application in an IDE with Tomcat or package it in a war and deploy it to Tomcat it works just fine.

I am guessing that the jetty plugin does not play nicely with Servlet 3 initializers.

Taking a look at your code I saw that you used Spring 4 and therefor I would urge you to take a look at Spring Boot.

If you use it you will be able to drop most of your Spring configuration files and also it has built in support for Embedded Tomcat and Jetty (I have not actually used this option) that work flawlessly both with IDEs and with build tools like Maven and Gradle.

geoand
  • 60,071
  • 24
  • 172
  • 190