5

I'm currently trying to shift my existing dynamic web project to Spring boot project and it uses web.xml for servlet mapping. I understand that spring would ignore the web.xml file, what should be the correct approach for spring to use the existing web.xml? And yes, I still need to stick to using web.xml for this project.

I'm kinda new to this, please guide me through! Thanks!

NatureWonder
  • 501
  • 1
  • 7
  • 10

2 Answers2

1

I suppose that you need to stick with a web.xml because your container uses an older version of Servlet than 3.0.

Spring Boot is built on Servlet 3.0. You have to update your main class to extend SpringBootServletInitializer and override configure method, which tells spring to use its Servlet 3.0 support. Embedded containers like Tomcat need Servlet 3.0, so if you want to start your project during the development process (including JUnit tests) in embedded containers, I think, from what I know, the only way is to rewrite your web.xml to Servlet 3.0 java config. But if you really want to deploy you app in an older container, you still can by using spring-boot-legacy module. It allows you to use web.xml for older containers; only thing you have to do is to add org.springframework.boot.legacy.context.web.SpringBootContextLoaderListener in your web.xml.

For more information about deploying war in an old container, take a look at Spring Boot's official documentation.

Spring Boot uses Servlet 3.0 APIs to initialize the ServletContext (register Servlets etc.) so you can’t use the same application out of the box in a Servlet 2.5 container. It is however possible to run a Spring Boot application on an older container with some special tools. If you include org.springframework.boot:spring-boot-legacy as a dependency (maintained separately to the core of Spring Boot and currently available at 1.0.2.RELEASE), all you should need to do is create a web.xml and declare a context listener to create the application context and your filters and servlets.

jantobola
  • 688
  • 2
  • 10
  • 28
  • Thanks for the input!. Im currently using servlet 3.0 and Tomcat 7.0.75. The reason I'm sticking to web.xml is because there are lots of servlet mapping in it and I just want to convert it to spring boot project first. I do have plans to replace the current web.xml with the proper one like (servletcontainerinitializer)? – NatureWonder Mar 02 '17 at 23:42
1

This is a very old question, but I'm currently on the same situation and I will share my experience.

I extended the SpringBootServletInitializer class to create my @SpringBootApplication, and it will INCLUDE your web.xml configuration by default. I'm still able to declare or edit existing servlet mapping, and it will be taken in account.