Gradle Spring Boot项目不能在Tomcat中作为WAR工作 [英] Gradle Spring Boot project not working in Tomcat as a WAR
问题描述
我有一个使用Gradle依赖关系在Spring Boot中编写的简单示例应用程序。它说helloworld调用localhost:8080 /问候。我将它打包为WAR,并将其作为myWebApp.war部署到Tomcat。
当我打电话给localhost:8080 / myWebApp /问候时,我得到了404。从下面推断catalina.log
2014年9月17日上午1:43:09 org.apache.catalina.core。 StandardEngine startInternal
INFO:启动Servlet引擎:Apache Tomcat / 7.0.54
2014年9月17日上午1:43:09 org.apache.catalina.startup.HostConfig deployWAR
信息:部署网站应用程序存档D:\C813507\Tomcat7\apache-tomcat-7.0.54\webapps\cftsample.war
2014年9月17日上午1:43:09 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没有加载。请参阅Servlet规范3.0,第10.7.2节。 Offending类:javax / servlet / Servlet.class
2014年9月17日上午1:43:09 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没有加载。请参阅Servlet规范3.0,第10.7.2节。 Offending类:javax / el / Expression.class
2014年9月17日上午1:43:13 org.apache.catalina.util.SessionIdGenerator createSecureRandom
INFO:创建SecureRandom实例以生成会话ID,使用[ SHA1PRNG]花了[138]毫秒。
2014年9月17日上午1:43:13 org.apache.catalina.startup.HostConfig deployWAR
INFO:部署Web应用程序存档D:\C813507\Tomcat7\apache-tomcat-7.0 .54\webapps\cftsample.war已在4,385 ms中完成
configure
方法来为您的应用程序提供配置类。这在关于将jar转换为war的入门指南 您通常会得到这样的类:
@Configuration
@EnableAutoConfiguration
公共类应用程序扩展SpringBootServletInitializer {
//作为可执行jar或war启动时使用
public static void main(String [] args)抛出异常{
SpringApplication.run(Application.class,args);
}
//在部署到独立的servlet容器时使用
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder应用程序){
return application.sources( Application.class);
}
}
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
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);
}
}
这篇关于Gradle Spring Boot项目不能在Tomcat中作为WAR工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!