Spring Boot WAR在Tomcat 7中不起作用,但在Tomcat 8中工作

时间:2016-08-05 12:38:48

标签: java spring maven tomcat spring-boot

我正在尝试让Spring Boot + WAR在我们的测试环境中运行。测试环境安装了Tomcat 7,部署时出现以下错误:

Aug 05, 2016 2:33:19 PM org.apache.catalina.loader.WebappClassLoader validateJarFile
INFO: validateJarFile(/var/lib/tomcat7/webapps/prt/WEB-INF/lib/tomcat-embed-core-8.0.33.jar) - jar not loaded. See Servlet Spec 3.0, section 10.7.2. Offending class: javax/servlet/Servlet.class
Aug 05, 2016 2:33:19 PM org.apache.catalina.loader.WebappClassLoader validateJarFile
INFO: validateJarFile(/var/lib/tomcat7/webapps/prt/WEB-INF/lib/tomcat-embed-el-8.0.33.jar) - jar not loaded. See Servlet Spec 3.0, section 10.7.2. Offending class: javax/el/Expression.class

现在奇怪的是,同样的WAR正在我的本地Tomcat 8上运行。 有谁知道为什么我的Tomcat 7实例不接受WAR? 我已经尝试在pom.xml中排除tomcat,我在SpringBootServletInitializer中使用了configure()方法。

2 个答案:

答案 0 :(得分:0)

检查以下配置:

的pom.xml

<properties>
    <tomcat.version>7.0.78</tomcat.version>
    <servlet-api.version>3.0.1</servlet-api.version>
</properties>

<dependencies>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>${servlet-api.version}</version>
        <scope>provided</scope>  <!-- provided: tomcat/lib/servlet-api.jar -->
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-juli</artifactId>
        <version>${tomcat.version}</version>
        <scope>runtime</scope> <!-- runtime: tomcat/bin/tomcat-juli.jar -->
    </dependency>
    <!-- remove it:
     <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    -->
</dependencies>

传统部署

要创建可部署的war文件(Tomcat 7),请阅读本指南:https://docs.spring.io/spring-boot/docs/current/reference/html/howto-traditional-deployment.html

  • <packaging>war</packaging>
  • 覆盖:SpringBootServletInitializer.configure

    @SpringBootApplication
    public class Application extends SpringBootServletInitializer {
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(Application.class);
        }
        public static void main(String[] args) throws Exception {
            SpringApplication.run(Application.class, args);
        }
    }
    

答案 1 :(得分:-1)

解决方案1.使用外部tomcat(你不能使用嵌入式tomcat)

  <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter-web</artifactid>
            <exclusions>
                <exclusion>
                    <groupid>org.springframework.boot</groupid>
                    <artifactid>spring-boot-starter-tomcat</artifactid>
                </exclusion>
            </exclusions>
     </dependency>

解决方案2.设置tomcat版本

   <properties>
            <tomcat.version>7.0.59</tomcat.version>
     </properties>