1

I generated a simple Greeting web application using Spring Initializr and choose war as the packaging type. Starting it from the terminal with mvn spring-boot:run and pointing my browser to http://localhost:8080/greeting returns the expected response.

I followed this article for the deployment steps and modified pom.xml so that to avoid including version numbers in the generated war:

<build>
    <finalName>${artifactId}</finalName>
    <plugins>
...
</build>

I build the war as usual with mvn clean installand got the expected demo-spring-web.war which I copied/pasted inside the apache-tomcat-10.0.4/webapp folder.

Then I started the Tomcat bu running catalina.sh run from the Tomcat bin folder. Tomcat started and displayed the demo-spring-web.war to be successfully deployed:

11-Apr-2021 18:24:36.414 INFO [main] org.apache.catalina.startup.HostConfig.deployWAR 
Deployment of web application archive [/Users/serguei/soft/apache-tomcat-10.0.4/webapps/demo-spring-web.war] has finished in [1,286] ms

But when I tried to access the deployed application at localhost:8080/demo-spring-web/greeting, it returned 404 status.

What's wrong with that?

belgoros
  • 3,590
  • 7
  • 38
  • 76
  • try just http://localhost:8080/greeting – Marek Apr 11 '21 at 17:13
  • Even if it is totally non-sense, I already tried it and without success, - every deployed app is running in its own context, that's why you should always prefix a route with the app name if you didn't tweak some Tomcat settings. – belgoros Apr 11 '21 at 17:48
  • 1
    Spring's `javax.servlet.ServletContainerInitializer` is silently ignored by Tomcat 10, which looks for a `jakarta.servlet.ServletContainerInitializer`. – Piotr P. Karwasz Apr 11 '21 at 17:50
  • Thanks a lot to @PiotrP.Karwasz, downgrading the Tomcat version to `9.0.45` version fixed the problem. Damned :). – belgoros Apr 11 '21 at 17:58
  • Well, Oracle's refusal to allow Jakarta EE to retain the `javax.*` namespace is causing thousands of hours of frustration to those, which don't follow Java EE news too often. – Piotr P. Karwasz Apr 11 '21 at 18:42

2 Answers2

0

You must declare a run class that extends SpringBootServletInitializer, for deploy in war mode.Like this :

@SpringBootApplication public class AppTomcat extends SpringBootServletInitializer {
}
kohane15
  • 809
  • 12
  • 16
Andre Kouame
  • 139
  • 1
  • 7
-1
  1. The default prepared/initialized application from spring initializer contains main() method initializing the application so that it can run in a stand alone mode and can be tested quickly. In case you want to deploy it to another container you should make a servlet initializing the application.

So you should be extending the class having main method with SpringBootServletInitializer and remove main method.

  1. The "mvn clean package" will fail if you have plugin "org.springframework.boot" in the .

I think following above two steps will make the built war deployable in external tomcat.

I had read an article some times back, may be that can also help: https://www.baeldung.com/spring-boot-war-tomcat-deploy