小编典典

Gradle Spring Boot项目无法在Tomcat中作为WAR工作

spring-boot

我有一个使用Gradle依赖关系用Spring Boot编写的简单示例应用程序。它在呼叫localhost:8080 /
greetings时显示helloworld。我将其打包为WAR,并将其作为myWebApp.war部署到Tomcat。

当我致电localhost:8080 / myWebApp / greetings时,我得到404。我应该从下面的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

阅读 434

收藏
2020-05-30

共1个答案

小编典典

要在独立的servlet容器中运行Spring
Boot应用程序,您需要告诉容器如何启动应用程序。为此,您可以扩展SpringBootServletInitializer和覆盖此configure方法以为您的应用程序提供配置类。在将jar转换为战争的入门指南中对此进行了描述。

您通常以这样的类结束:

@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);
    }
}
2020-05-30