手记

Spring Boot 将jar包发布方式改为War包

Spring Boot 将jar包发布方式改为War包

参考课程 Spring Boot 热部署

1.修改pom.xml中的jar
    <groupId>com.imooc</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <!--<packaging>jar</packaging>-->
    <packaging>war</packaging>
2.引入tomcat依赖
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope><!--在Ide中调试时需要注销掉该scope,否则项目启动不起来-->
        </dependency>
3.修改入口类Application.java
  • 继承SpringBootServletInitializer类
  • 重写protected SpringApplicationBuilder configure(SpringApplicationBuilder builder)方法
@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer{

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder)
    {
        return builder.sources(DemoApplication.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}
4.生成war包

<br><br>

二、问题总结
1. 部署war包报错
严重: The ServletContentInitializer [# Licensed to the Apache Software Foundatio
n (ASF) under one or more] could not be created
java.lang.ClassNotFoundException: # Licensed to the Apache Software Foundation (
ASF) under one or more
        at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoa
der.java:1713)
        at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoa
der.java:1558)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:348)
        at org.apache.catalina.startup.ContextConfig.getServletContainerInitiali
zer(ContextConfig.java:1659)
        at org.apache.catalina.startup.ContextConfig.processServletContainerInit
ializers(ContextConfig.java:1569)
        at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.jav
a:1277)
        at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfi
g.java:878)
        at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfi
g.java:369)
        at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(Lifecycl
eSupport.java:119)
        at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBa
se.java:90)
        at org.apache.catalina.core.StandardContext.startInternal(StandardContex
t.java:5269)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase
.java:901)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:87
7)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)

        at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:977)

        at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:
1655)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:51
1)
        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)

解决方案1:

提高 tomcat版本

解决方案2:

降低Spring Boot版本,并配置tomcat版本,但是这个会带来其他一些错误,需要逐个解决。

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <tomcat.version>7.0.73</tomcat.version><!--配置这里-->
    </properties>
5人推荐
随时随地看视频
慕课网APP