Google App Engine and Spring MVC 5

I've tried new Spring Framework (Wweb MVC 5) with Standard AppEngine runtime (Java8). Unfortunately "most standard" approach to initialise application with extending AbstractAnnotationConfigDispatcherServletInitializer  class has failed miserably:

public class EntryPoint extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[]{RootConf.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[]{WebConf.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
}

The failure was (it really seems to be platform rather than Spring issue):
org.eclipse.jetty.annotations.ServletContainerInitializersStarter doStart:  (ServletContainerInitializersStarter.java:67)
java.lang.IllegalArgumentException: Failed to register servlet with name 'dispatcher'.Check if there is another servlet registered under the same name.
at org.springframework.util.Assert.notNull(Assert.java:193)
at org.springframework.web.servlet.support.AbstractDispatcherServletInitializer.registerDispatcherServlet(AbstractDispatcherServletInitializer.java:100)

The (unfortunate, but working) solution is to use web.xml to initialise both the Root and Servlet WebApplicatrionContexts.

<?xml version="1.0" encoding="utf-8"?>
<web-app>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </context-param>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>name.prokop.bart.gpgsend.RootConfig</param-value>
    </context-param>

    <servlet>
        <servlet-name>dispatcher</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>name.prokop.bart.gpgsend.WebConfig</param-value>
        </init-param>
    </servlet>    
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

The above is almost identical to using AbstractAnnotationConfigDispatcherServletInitializer. Customisation can be achieved with implementing WebMvcConfigurer interface. For example:

@Configuration
@EnableWebMvc
@ComponentScan("name.prokop.bart.gpgsend.mvc")
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addRedirectViewController("/", "http://google.com");
    }

}

Comments

Popular posts from this blog

Hardening OpenWRT - adding non-root user account

Dropbear SSH keys and autossh on OpenWRT

SSH Tunel with OpenWRT