Gradle Spring Boot project not working in Tomcat as a WAR
Asked Answered
H

2

5

I have a simple sample application written in Spring Boot using Gradle dependency. It says helloworld on calling localhost:8080/greetings. I packaged it as WAR and deployed it to a Tomcat as a myWebApp.war.

When i call localhost:8080/myWebApp/greetings i get 404. What am i supposed to infer from the below catalina.log

  Sep 17, 2014 1:43:09 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.54
Sep 17, 2014 1:43:09 AM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deploying web application archive D:\C813507\Tomcat7\apache-tomcat-7.0.54\webapps\cftsample.war
Sep 17, 2014 1:43:09 AM org.apache.catalina.loader.WebappClassLoader validateJarFile
INFO: validateJarFile(D:\C813507\Tomcat7\apache-tomcat-7.0.54\webapps\cftsample\WEB-INF\lib\tomcat-embed-core-7.0.54.jar) - jar not loaded. See Servlet Spec 3.0, section 10.7.2. Offending class: javax/servlet/Servlet.class
Sep 17, 2014 1:43:09 AM org.apache.catalina.loader.WebappClassLoader validateJarFile
INFO: validateJarFile(D:\C813507\Tomcat7\apache-tomcat-7.0.54\webapps\cftsample\WEB-INF\lib\tomcat-embed-el-7.0.54.jar) - jar not loaded. See Servlet Spec 3.0, section 10.7.2. Offending class: javax/el/Expression.class
Sep 17, 2014 1:43:13 AM org.apache.catalina.util.SessionIdGenerator createSecureRandom
INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [138] milliseconds.
Sep 17, 2014 1:43:13 AM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deployment of web application archive D:\C813507\Tomcat7\apache-tomcat-7.0.54\webapps\cftsample.war has finished in 4,385 ms
Heaves answered 17/9, 2014 at 6:47 Comment(0)
R
19

To run a Spring Boot application in a standalone servlet container you need to tell the container how to launch the application. You do so by extending SpringBootServletInitializer and overriding the configure method to provide the configuration classes for your application. This is described in the getting started guide on converting a jar to a war.

You typically end up with a class like this:

@Configuration
@EnableAutoConfiguration
public class Application extends SpringBootServletInitializer {

    // Used when launching as an executable jar or war
    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }

    // Used when deploying to a standalone servlet container
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }
}
Reality answered 17/9, 2014 at 8:45 Comment(2)
Thanks, so much clearer and more concise than Spring Boot's own documentation!Markup
excellent. I found many people having the same issue while working with Spring boot first time.Groceries
S
0

In my case, the server used java 1.8 and compile with java 11, not working, when compile with java 1.8, it works!!

Saxton answered 4/7, 2022 at 0:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.