Tomcat中的spring-boot应用程序战争部署,现有的spring web应用程序失败

Posted

技术标签:

【中文标题】Tomcat中的spring-boot应用程序战争部署,现有的spring web应用程序失败【英文标题】:spring-boot app war deployment in Tomcat , existing spring web app fails 【发布时间】:2016-03-23 16:56:16 【问题描述】:

在我的tomcat 中包含现有的spring Web 应用程序(它不使用spring boot),现在我开发了新的应用程序使用spring boot,当我部署新的spring boot 应用程序时,我现有的spring 应用程序无法运行, (它显示了一些东西ClassNotFoundException)。我的新 spring boot 应用程序正在运行。我正在使用maven 构建工具。 谢谢。

这是我用于 Spring Boot 的 pom 文件

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>

        <groupId>com.test</groupId>
        <artifactId>test-app</artifactId>
        <version>0.0.1-SNAPSHOT</version>

        <name>Test-App</name>
        <description>Test App</description>

        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.3.0.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>

        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <java.version>1.8</java.version>
            <guava.version>16.0.1</guava.version>
            <start-class>com.test.Application</start-class>
        </properties>

        <dependencies>

         <!-- spring-boot-starter-data-jpa dependency -->

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>


            <!-- spring-boot-starter-hateoas dependency -->

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-hateoas</artifactId>
            </dependency>

            <!-- spring-boot-starter-web dependency -->

            <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>

            <!-- postgresql dependency -->

            <dependency>
                <groupId>org.postgresql</groupId>
                <artifactId>postgresql</artifactId>
                <version>9.4-1201-jdbc41</version><!--$NO-MVN-MAN-VER$-->
                <scope>runtime</scope>
            </dependency>

            <!-- test dependency -->

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>

            <!-- spring security dependency -->

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-security</artifactId>
                <version>1.1.9.RELEASE</version><!--$NO-MVN-MAN-VER$-->
            </dependency>
            <dependency>
                <groupId>org.springframework.security.oauth</groupId>
                <artifactId>spring-security-oauth2</artifactId>
                <version>2.0.7.RELEASE</version><!--$NO-MVN-MAN-VER$-->
            </dependency>
            <dependency>
                <groupId>org.springframework.security</groupId>
                <artifactId>spring-security-jwt</artifactId>
                <version>1.0.3.RELEASE</version><!--$NO-MVN-MAN-VER$-->
            </dependency>   

            <!-- jayway  dependency -->

            <dependency>
                <groupId>com.jayway.jsonpath</groupId>
                <artifactId>json-path</artifactId>
                <version>2.0.0</version><!--$NO-MVN-MAN-VER$-->
            </dependency>

            <!-- Guava dependency -->

            <dependency>
                <groupId>com.google.guava</groupId>
                <artifactId>guava</artifactId>
                <version>19.0-rc2</version>
             </dependency>

            <!-- Java EE dependency -->

            <dependency>
                <groupId>javax.inject</groupId>
                <artifactId>javax.inject</artifactId>
                <version>1</version>
            </dependency>


            <!-- swagger dependency -->

            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>2.2.2</version>
            </dependency>
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>2.2.2</version>
            </dependency>

        <!-- lombok dependency -->

            <dependency> 
                <groupId>org.projectlombok</groupId> 
                <artifactId>lombok</artifactId> 
                <version>1.16.6</version> 
                <scope>provided</scope> 
            </dependency>
            <!-- jose4j dependency for oauth -->
            <dependency>
                <groupId>org.bitbucket.b_c</groupId>
                <artifactId>jose4j</artifactId>
                <version>0.4.4</version>
            </dependency>

        <!-- spring-context-support dependency for cachemanager --> 
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
            </dependency>


            <!-- jettison dependency  -->   
            <dependency>
                <groupId>org.codehaus.jettison</groupId>
                <artifactId>jettison</artifactId>
                <version>1.3.7</version>
            </dependency>

            <!-- spring-boot-starter-tomcat dependency  --> 

            <!-- Add tomcat only if I want to run directly -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
                <scope>provided</scope>
            </dependency>
</dependencies>
</project>

这是我的 spring mvc pom 文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.test.auth</groupId>
      <artifactId>Oauth</artifactId>
      <packaging>war</packaging>
      <version>0.0.1-SNAPSHOT</version>
      <name>Oauth Maven Webapp</name>
      <url>http://maven.apache.org</url>
    <properties>
            <hibernate.version>4.3.6.Final</hibernate.version>
            <!-- Logging -->
            <logback.version>1.0.13</logback.version>
            <slf4j.version>1.7.5</slf4j.version>
        </properties>

        <repositories>
            <repository>
                <id>maven2-repository.java.net</id>
                <name>Java.net Repository for Maven</name>
                <url>http://download.java.net/maven/2/</url>
            </repository>
        </repositories>

        <dependencies>
            <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.11</version>
          <scope>test</scope>
        </dependency>
            <dependency>
        <groupId>org.bitbucket.b_c</groupId>
        <artifactId>jose4j</artifactId>
        <version>0.4.3</version>
      </dependency>
            <dependency>
                <groupId>commons-lang</groupId>
                <artifactId>commons-lang</artifactId>
                <version>2.6</version>
            </dependency>

            <!-- Jersey -->
            <dependency>
                <groupId>com.sun.jersey</groupId>
                <artifactId>jersey-server</artifactId>
                <version>1.8</version>
            </dependency>
            <dependency>
                <groupId>com.sun.jersey.contribs</groupId>
                <artifactId>jersey-multipart</artifactId>
                <version>1.8</version>
            </dependency>
            <!-- Spring 3 dependencies -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>4.0.5.RELEASE</version>
            </dependency>

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>4.0.5.RELEASE</version>
            </dependency>

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>4.0.5.RELEASE</version>
            </dependency>

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>4.0.5.RELEASE</version>
            </dependency>


            <!-- Jersey + Spring -->
            <dependency>
                <groupId>com.sun.jersey.contribs</groupId>
                <artifactId>jersey-spring</artifactId>
                <version>1.8</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-core</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-web</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-beans</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-context</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>



            <!-- jakson -->
            <dependency>
                <groupId>com.sun.jersey</groupId>
                <artifactId>jersey-json</artifactId>
                <version>1.8</version>
            </dependency>
            <!-- Spring Security -->

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>3.2.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>3.2.3.RELEASE</version>
        </dependency>



        <!-- Hibernate -->
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-entitymanager</artifactId>
                <version>$hibernate.version</version>
            </dependency>
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-core</artifactId>
                <version>$hibernate.version</version>
            </dependency>
            <dependency>
                    <groupId>org.postgresql</groupId>
                     <artifactId>postgresql</artifactId>
                     <version>9.3-1100-jdbc41</version>
                    </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.9</version>
            </dependency>
            <dependency>
                <groupId>commons-dbcp</groupId>
                <artifactId>commons-dbcp</artifactId>
                <version>1.4</version>
            </dependency>
        <!-- Servlet Context -->

        <dependency>
              <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
    </dependency>


            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-tx</artifactId>
                <version>4.0.5.RELEASE</version>
            </dependency>
    <!-- Spring ORM support -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-orm</artifactId>
                <version>4.0.5.RELEASE</version>
            </dependency>


            <dependency>
        <groupId>ph.samson</groupId>
        <artifactId>cors-filter</artifactId>
        <version>1.5.0.1</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-dbcp2</artifactId>
                <version>2.0.1</version>
            </dependency>
    </dependencies>
      <build>
        <finalName>Oauth</finalName>
      </build>
    </project>

完整的堆栈跟踪

java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
        org.jboss.logging.Slf4jLoggerProvider.getLogger(Slf4jLoggerProvider.java:29)
        org.jboss.logging.LoggerProviders.find(LoggerProviders.java:33)
        org.jboss.logging.LoggerProviders.&lt;clinit&gt;(LoggerProviders.java:28)
        org.jboss.logging.Logger.getLogger(Logger.java:2163)
        org.jboss.logging.Logger$1.run(Logger.java:2263)
        java.security.AccessController.doPrivileged(Native Method)
        org.jboss.logging.Logger.getMessageLogger(Logger.java:2227)
        org.jboss.logging.Logger.getMessageLogger(Logger.java:2214)
        org.hibernate.cfg.Configuration.&lt;clinit&gt;(Configuration.java:192)
        org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:324)
        com.cherry.oauth.multitenant.connection.provider.MultiTenantConnectionProvider.getSessionFactory(MultiTenantConnectionProvider.java:80)
        com.cherry.oauth.multitenant.dbhandler.TenantSesionFactory.getSession(TenantSesionFactory.java:49)
        com.cherry.oauth.service.MyAuthServiceImpl.authenticate(MyAuthServiceImpl.java:89)
        sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        java.lang.reflect.Method.invoke(Method.java:483)
        com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
        com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ObjectOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:258)
        com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
        com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)
        com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
        com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
        com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
        com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
        com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469)
        com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400)
        com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
        com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
        com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
        com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
        com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:699)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
        org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
        org.apache.catalina.filters.CorsFilter.handleSimpleCORS(CorsFilter.java:301)
        org.apache.catalina.filters.CorsFilter.doFilter(CorsFilter.java:169)
    </pre><p><b>root cause</b></p><pre>java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
        org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1333)
        org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1167)
        org.jboss.logging.Slf4jLoggerProvider.getLogger(Slf4jLoggerProvider.java:29)
        org.jboss.logging.LoggerProviders.find(LoggerProviders.java:33)
        org.jboss.logging.LoggerProviders.&lt;clinit&gt;(LoggerProviders.java:28)
        org.jboss.logging.Logger.getLogger(Logger.java:2163)
        org.jboss.logging.Logger$1.run(Logger.java:2263)
        java.security.AccessController.doPrivileged(Native Method)
        org.jboss.logging.Logger.getMessageLogger(Logger.java:2227)
        org.jboss.logging.Logger.getMessageLogger(Logger.java:2214)
        org.hibernate.cfg.Configuration.&lt;clinit&gt;(Configuration.java:192)
        org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:324)
        com.cherry.oauth.multitenant.connection.provider.MultiTenantConnectionProvider.getSessionFactory(MultiTenantConnectionProvider.java:80)
        com.cherry.oauth.multitenant.dbhandler.TenantSesionFactory.getSession(TenantSesionFactory.java:49)
        com.cherry.oauth.service.MyAuthServiceImpl.authenticate(MyAuthServiceImpl.java:89)
        sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        java.lang.reflect.Method.invoke(Method.java:483)
        com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
        com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ObjectOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:258)
        com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
        com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)
        com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
        com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
        com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
        com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
        com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469)
        com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400)
        com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
        com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
        com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
        com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
        com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:699)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
        org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
        org.apache.catalina.filters.CorsFilter.handleSimpleCORS(CorsFilter.java:301)
        org.apache.catalina.filters.CorsFilter.doFilter(CorsFilter.java:169)

【问题讨论】:

发布完整错误! 对不起我的英语不好, 当我将 Spring Boot 应用程序 War 部署到 tomcat 8.0.30 时,现有应用程序无法运行。它显示 ClassNotFound 异常。怎么解决,谢谢 它们是否共享相同的上下文路径? 向我们展示完整的堆栈跟踪? 【参考方案1】:

在 spring boot 应用程序中使用 1.3.0.RELEASE,它有休眠版本 4.6.11.Final, 现有 spring mvc 应用使用 4.3.6.决赛,

解决方案-1 spring mvc app版本要升级,(hibernate 4.6.11.Final) 解决方案 2 Spring Boot 应用程序的降级版本。 (spring boot 父版本 1.1.8.RELEASE)

【讨论】:

【参考方案2】:

似乎 hibernate 正在做某种类共享 @tomcat 容器级别,您的 spring-mvc 应用程序不支持,但 spring-boot 应用程序支持。在这里,如果您看到错误跟踪,您会发现休眠配置需要流行的日志框架外观库,即 slf4j,而您的 spring-mvc 应用程序中缺少该库(尽管我在您的 pom 上看到了&lt;slf4j.version&gt;1.7.5&lt;/slf4j.version&gt;,但它没有在任何地方使用)。要解决抛出的错误,请尝试在您的 spring-mvc 应用程序中包含以下依赖项:-

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.13</version>
</dependency>

【讨论】:

以上是关于Tomcat中的spring-boot应用程序战争部署,现有的spring web应用程序失败的主要内容,如果未能解决你的问题,请参考以下文章

如何在外部 tomcat 中部署 Spring-boot REST API

使用war将spring-boot和angular 7应用程序部署到tomcat 8.5中

无法在一台特定服务器上部署战争

向tomcat部署战争

spring-boot 战争在启动时失败

在 osgi 文件夹中复制后,Liferay 7 tomcat 中的战争在哪里爆炸