SpringBoot打war包部署Tomcat(最全)

1. tomcat9
2. jdk8
3. springboot2.x

1 - pom.xml 修改打包方式

    <packaging>war</packaging>

2 - 加入SpringBoot打包插件(pom.xml)

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

3 - 在打包插件中加入配置SpringBoot的入口类的标签名

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <!--配置springboot入口类-->
                <configuration>
                    <fork>true</fork>
                    <jvmArguments>Dfile.encoding=UTF-8</jvmArguments>
                    <!--配置入口类的标签名-->
                    <mainClass>com.kaiguo.Test</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>


                <mainClass>com.kaiguo.Test</mainClass>

这个必须要修改成你自己的配口类标签名

4 - 依赖的修改(pom.xml)

因为打war在tomcat部署,我们需要将内嵌的tomcat去掉
加入你的springboot有jsp文件的话还要将tomcat解析jsp的依赖去掉

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--打包不参与-->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
        <!--打包不参与,也就是打包去掉tomcat-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>


        <scope>provided</scope>

这个scope的意思在当前环境可以使用,但是不参与打包!!!

5 - 修改主配置类(用于依赖外部tomcat)

@SpringBootApplication
public class Test extends SpringBootServletInitializer {

    @Override  //这个表示使用外部的tomcat容器
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        // 注意这里要指向原先用main方法执行的启动类
        return builder.sources(Test.class);
    }
    public static void main(String[] args) {
        SpringApplication.run(Test.class,args);
    }
}

最后进行打包即可

6 - 测试war包

将war包放入tomcat下的webapps下面
我们启动tomcat
在这里插入图片描述

启动tomcat
在这里插入图片描述

在这里插入图片描述
注意、:war部署的时候 tomcat默认将你的根路径变成你的war的名称

访问我们的测试接口
在这里插入图片描述

成功

注意:war部署的时候 tomcat默认将你的根路径变成你的war包的名称

例如 你的war是 test.war
那么部署的时候访问接口必须是
http://localhost:8080/test/

我的war包是wa.war
那么部署之后 我要访问我的接口/consumer/hello
必须是 http://localhost:8080/wa/consumer/hello而不是http://localhost:8080/consumer/hello

很多小伙伴经常搞错

猜你喜欢

转载自blog.csdn.net/m0_60721514/article/details/125244084
今日推荐