zoukankan      html  css  js  c++  java
  • spring boot + jersey工程由jar包转为war包在tomcat中启动报错问题

    第一步:
      在maven下,将Spring Boot工程由jar转换为war包启动,很简单,将pom.xml文件中的packaging改为war
        <packaging>war</packaging>
      如果你使用Gradle,你需要修改build.gradle来将war插件应用到项目上:
        apply plugin: 'war'
    第二步:
      产生一个可部署war包的第一步是提供一个SpringBootServletInitializer子类,并覆盖它的configure方法。这充分利用了Spring框架对Servlet 3.0的支持,并允许你在应用通过servlet容器启动时配置它。通常,你只需把应用的主类改为继承SpringBootServletInitializer即可:
        @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);
        }
        }
    然后根据正常方式部署到应用服务器下,正常情况下应该就完成了jar包到war包的转换。

    有几点需要说明,网上许多资料中(包括官方文档)说需要将artifactId为spring-boot-starter-tomcat的组件scope写为provided,即

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

    原因是内嵌tomcat会与部署使用的外部tomcat起冲突,在某种情况下可能是会出问题,但是我经过测试证明,至少在我的工程中不将内嵌tomcat写为provided也是可以运行的,这样有一个好处就是,通过main方法可以直接启动,如果内嵌tomcat为provided,main方法是无法直接启动的,所以在开发时可以先自己尝试下,方便开发,等到部署时,保险起见可以调成provided。

    因为我搭建了jar包的工程并且通过main方法成功启动实现了功能,原以为转为war包会很快,结果发生了错误,花了将近一天的时间才解决问题。

    我是部署在tomcat下,使用jersey实现rest,结果启动发生了如下错误,

    SEVERE: A child container failed during start
    java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/yishi-service]]
    at java.util.concurrent.FutureTask.report(FutureTask.java:122)
    at java.util.concurrent.FutureTask.get(FutureTask.java:192)
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:915)
    at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:871)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1407)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1397)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
    Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/yishi-service]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:153)
    ... 6 more
    Caused by: org.apache.catalina.LifecycleException: Failed to start component [org.apache.catalina.webresources.StandardRoot@6e37288d]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:153)
    at org.apache.catalina.core.StandardContext.resourcesStart(StandardContext.java:4958)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5088)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
    ... 6 more

     找了很多资料,各种原因都有,但是都不能解决我的问题,

    包括通过官方网页http://start.spring.io/ 获取jersey组件的war包都是有问题的,为了这个问题还换了一个更快速的FQ工具。。。。。

    于是我从spring boot fat jar中找问题,终于发现了问题所在,我使用的spring boot 版本为:1.4.2.RELEASE,添加了spring-boot-starter-jersey,其中有两项组件使工程在tomcat中通过war包形式启动报错,一项为<groupId>org.glassfish.jersey.ext</groupId> <artifactId>jersey-bean-validation</artifactId>,由于我不会使用到该组件,因此直接排除了,还有一项为<groupId>org.glassfish.jersey.ext</groupId><artifactId>jersey-spring3</artifactId> ,此项为jersey继承spring,要想jersey管理的bean使用spring的bean,必须使用,此版本为2.23.2,会导致工程启动不了,将版本改为2.22.1即可使用了。简化代码如下:

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-jersey</artifactId>
      <exclusions>
      <exclusion>
      <groupId>org.glassfish.jersey.ext</groupId>
      <artifactId>jersey-bean-validation</artifactId>
      </exclusion>
      <exclusion>
      <groupId>org.glassfish.jersey.ext</groupId>
      <artifactId>jersey-spring3</artifactId>
      </exclusion>
      </exclusions>
    </dependency>

    <dependency>
    <groupId>org.glassfish.jersey.ext</groupId>
    <artifactId>jersey-spring3</artifactId>
    <version>2.22.1</version><!--$NO-MVN-MAN-VER$-->
    </dependency>

    至此,问题就解决了,希望使用spring boot + jersey 搭建工程的小伙伴,不再踩此坑。

    此种方式虽能解决问题,但是我发现造成此问题发生的根本问题是我本地的maven仓库中jar文件有问题导致的,当我清除repository之后,重新download之后,问题解决了,可以直接使用官方的spring-boot-starter-jersey fat jar包

  • 相关阅读:
    [NoiPlus2016]天天爱跑步
    POJ3539 Elevator
    CodeForces 37E Trial for Chief
    CodeForces 986C AND Graph
    [tyvj-2054][Nescafé29]四叶草魔杖 费用流
    [CodeForces]986A Fair
    [CodeForces]981C Useful Decomposition
    分配问题
    圆桌问题
    数字梯形问题
  • 原文地址:https://www.cnblogs.com/vincent0928/p/6095619.html
Copyright © 2011-2022 走看看