Spring Boot and Tomcat7

I had an issue with Spring Boot app @ Tomcat7 – the app was running fine at Tomcat8, but at Tomcat7 it wasn’t starting.

The app is using web.xml-less method – some class extends SpringBootServletInitializer and is annotated with @SpringBootApplication annotation. Again, this is sufficient at Tomcat8 for some reason, but not for Tomcat7.

Tomcat7 is Servlet 3.0, Spring Boot recent versions use Servler 3.1, as is Tomcat8, and this was part of the problem – because maven generated eclipse project was really willing to use Servlet 3.1 facet, which made it impossible to deploy to Tomcat7 with WTP.

In order to fix that, not only servlet version 3.0.1 but also Tomcat version 7.0.x had to be declared in pom. Then eclipse project had to be deleted (with all it’s dot meta files and folders) and recreated – this made it possible for me to at least deploy to Tomcat7 from Eclipse with WTP. Whew.

But – the app wasn’t starting. It was deployed, but Spring servlet didn’t register, no Spring contexts were initialized, etc.

It took me several hours to find a solution – and I’ve stumbled over it almost accidentally online. It turns out it’s insufficient for your @SpringBootApplication annotated class to just extend SpringBootServletInitializer – it should also explicitly implement WebApplicationInitializer! That’s it! This has fixed the issue – the app was running again.

I guess this is somehow required by the whole @HandlesTypes driven thing in servlet 3.0, but it is quite weird it works differently in servlet 3.1. Or is it just Tomcat? Well, apparently people are complaining about the same issue on… WebLogic.

Anyhow, the solution is found.

P.S. Just when I thought I’m done – I’ve run into same issue again, but on different environment – the problem moved from dev to test. Damn! I also had a bit older Tomcat on test, so checked with that version locally – worked fine. So took me a bit more to figure out switch from Java7 to Java8 is needed to fix the problem. Now I’m absolutely puzzled regarding the causes, but anyhow, the WebApplicationInitializer and Java8 made it work. Finally!