javaxmlspringservletsspring-boot

SpringBoot WEB-INF not loaded


I have a spring boot application. Packaged as a war file, such that the contents are as follows

static
org - springframework - boot -loader - SpringClasses
META-INF
    - MANIFEST.MF
    - maven -my.group.id - my-artifact-id - pom.xml & pom.properties
WEB-INF
    - lib (contains all jars)
    - classes (my main application's classes)
    - some other stuff
    - web.xml
    - main-servlet.xml

Where web.xml and main-servlet.xml are the configuration xml's

I tried, from the springBoot application doing as follows:

@EnableWebMvc
@EnableAutoConfiguration
@Configuration
@ImportResource({ "classpath:main-servlet.xml"})
public class FakeAppBooter {

    public static void main(String args[]) {
        SpringApplication.run(FakeAppBooter.class, args);
        System.out.println("Test");
    }

    public DispatcherServlet mvcDispatcherServlet() {
            XmlWebApplicationContext ctx = new XmlWebApplicationContext();
            ctx.setConfigLocation("classpath:main-servlet.xml");
            DispatcherServlet dispatcherServlet = new DispatcherServlet(ctx);
            return dispatcherServlet;
        }

        @Bean
        public ServletRegistrationBean mvcServletRegistrationBean() {
            ServletRegistrationBean bean = new ServletRegistrationBean();
            bean.setServlet(mvcDispatcherServlet());
            ArrayList<String> list = new ArrayList<>();
            list.add("/");
            bean.setUrlMappings(list);
            return bean;
        }
}

However on startup I get :

Caused by: java.io.FileNotFoundException: class path resource [main-servlet.xml] cannot be opened because it does not exist
        at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:329)
        ... 25 more

I need these to define the servlets for my application.

what should I do?


Solution

  • Solution for your Question:

    1. While you are using SpringBoot Never care about your web.xml, Because SpringBoot itself will initialize your web Container through EmbeddedServletContainer preferably TomcatEmbeddedServletContainer. Hence ignore your web.xml configuration and it resolves your first question
    2. You haven't placed your main-servlet.xml in proper classpath. Copy your main-servlet.xml and paste it in src folder of your project.

    3. Change your code accordingly

      import java.util.ArrayList;
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
      //Place your Imports Appropriately
      
      /**
       * @author Praveen
       *
       */
        @EnableWebMvc
        @EnableAutoConfiguration(exclude=DataSourceAutoConfiguration.class)
        @ImportResource({ "classpath:main-servlet.xml"})
        @ComponentScan
        public class FakeAppBooter {
        private ApplicationContext ctx;
        public static void main(String[] args) {
        SpringApplication.run(FakeAppBooter.class, args);
        System.out.println("Test>>>>");
       }
        public DispatcherServlet mvcDispatcherServlet() {
           ctx = new ClassPathXmlApplicationContext();
           ((AbstractRefreshableConfigApplicationContext)                       ctx).setConfigLocation("classpath:main-servlet.xml");
           DispatcherServlet dispatcherServlet = new DispatcherServlet();
            return dispatcherServlet;
         }
      
        @Bean
          public ServletRegistrationBean mvcServletRegistrationBean() {
          ServletRegistrationBean bean = new ServletRegistrationBean();
          bean.setServlet(mvcDispatcherServlet());
          ArrayList<String> list = new ArrayList<>();
          list.add("/");
          bean.setUrlMappings(list); 
          return bean;
          }
        }
      
    4. main-servlet.xml
      I have kept the main-servlet.xml in a very simple manner.

      main-servlet.xml file

    5. Successful Log Below
      Successful Log