Tomcat WAR Deployment successful but returning 404 NOT FOUND
When we try to deploy a WAR file on external tomcat server, we sometimes get into an issue where the WAR deployment is successful but when we try to access the deployed application, it returns 404 NOT FOUND.
Let's know how to fix such an issue.
Why do we get such an error?
After successful WAR deployment, here are the few important things to look for in case of such an issue.
- Is the requested path available on the deployed application?
- Is the requested path correct (make sure it is case-sensitive) without any spelling mistakes?
If your answer to the above questions is YES, then we obvious reason is the application doesn't have a Servlets that resolve the requested paths.
If the application is a Spring Boot Application, then the below changes are required.
Fixing Issue on a Spring Boot Application
Spring Boot Application without Servlet Resolver
Here is the application, that works on local machine without any issues, but when we try to deploy it on external tomcat server, this doesn't resolve the paths that are part of this application.
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
Spring Boot Application with Servlet Resolver
The application must extend SpringBootServletInitializer
and must override the configure
method as shown below.
@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(DemoApplication.class);
}
}
Re-build, Re-Deploy and Test
After making the above changes, try to re-build the application and re-deploy the generated WAR file.
This time, the deployment should be successful and the paths should be accessible as shown below.
Conclusion
Now, we know how to fix the 404 NOT FOUND issue on a Spring Boot application deployed on an external tomcat using a WAR file.