4

I am trying to migrate my app's web.xml to Java based configuration. We are using spring 4.1, Java 7, Servlet 3.1, Tomcat 8 and Eclipse Luna. The web service framework is Jersey 2.14.

I used mainly the following guide: http://www.robinhowlett.com/blog/2013/02/13/spring-app-migration-from-xml-to-java-based-config/

I have created WebApplicationInitializer that follows the web.xml configuration, deleted the web.xml, configured Maven not to look for web.xml and did mvn clean install successfuly.

When i try to start tomcat i get the following error:

'Publishing to Tomcat v8.o Server at localhost...' has encountered a problem. Resource '/sb-server/target/m2e-wtp/web-resources/WEB-INF/web.xml' does not exist.

I have tried to clean tomcat directory but it didn't help and it looks like i have missed something since AFAIK Tomcat 8 should be Java-Based-Configuration-Friendly.

Did i miss a step in the migration?

former web.xml (that worked as expected):

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"> 

    <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>
            com.sb.configuration.ServerConfiguration
        </param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>Jersey REST Service</servlet-name>
        <servlet-class>
            org.glassfish.jersey.servlet.ServletContainer
        </servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>com.sb.configuration.RestJaxRsApplication</param-value>
        </init-param>
        <init-param>
            <param-name>jersey.config.server.tracing.type</param-name>
            <param-value>ALL</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>Jersey REST Service</servlet-name>
        <url-pattern>/api/*</url-pattern>
    </servlet-mapping>

    <!-- Spring Security -->
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy
        </filter-class>
    </filter>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

The WebApplicationInitializerImplemenatation:

@Order(Ordered.HIGHEST_PRECEDENCE)
public class WebAppInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(final ServletContext container) throws ServletException {

        // Set up application context
        final AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
        appContext.register(ServerConfiguration.class);
        container.addListener(new ContextLoaderListener(appContext));

        // Register listeners
        container.addListener(ContextLoaderListener.class);

        // Jersey Servlet configuration
        final ServletRegistration.Dynamic dispatcher =
                container.addServlet("Jersey REST Service", ServletContainer.class);
        dispatcher.setInitParameter("javax.ws.rs.Application", "com.sb.configuration.RestJaxRsApplication");

        dispatcher.setInitParameter("jersey.config.server.tracing.type", "ALL");
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/api/*");

        // Filters
        final Dynamic filterRegistration = container.addFilter("springSecurityFilterChain", DelegatingFilterProxy.class);
        filterRegistration.addMappingForUrlPatterns(null, false, "/*");

    }
}
Eyal
  • 1,748
  • 2
  • 17
  • 31

1 Answers1

0

There's a much simpler way to do it.

Add a dependency on Servlet 3.1 in your pom file

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
</dependency>

First create a Servlet initializer. Example below:

public class WebAppInit implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.scan(AppConfiguration.class.getPackage().getName());

        servletContext.addListener(new ContextLoaderListener(context));
        ServletRegistration.Dynamic appServlet = servletContext.addServlet("appServlet", new DispatcherServlet(context));

        appServlet.setLoadOnStartup(1);
        Set<String> mappings = appServlet.addMapping("/");

        if (!mappings.isEmpty()) {
            throw new IllegalStateException("Conflicting mappings found! Terminating. " + mappings);
        }
    }
}

Your Application configuration also needs some settings. Example below:

@Configuration
@EnableWebMvc
@ComponentScan("com.your.package.root")
public class AppConfiguration extends WebMvcConfigurerAdapter {

    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setOrder(0);
        return viewResolver;
    }

    /**
     * Spring and WEB settings from WebConfAdapt.
     */

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
}

Other than that, no more configuration should be needed. I successfully started my Tomcat with this. (However, this was on Tomcat 7)

Robin Jonsson
  • 2,761
  • 3
  • 22
  • 42
  • Hi, thanks for the answer, but as i said i am working with Jersey rather than Spring MVC. In my configuration, the maven dependency suggested by you already exists, also WebApplicationConfiguration implementation exists. I will add the relevant code to the question. – Eyal Mar 04 '15 at 08:53