两个步骤,打包war,tomcat使用war包

资源代码:

https://download.csdn.net/download/hashiqimiya/92455258

如上

了解资源代码:

写了一个controller代码,控制接口对应该运行的函数。

复制代码
package org.example.testproducttomcatwar;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
    @GetMapping("/hello")
    public String hello() {
        return "Spring Boot WAR 项目成功运行!";
    }
}

application的代码:

复制代码
package org.example.testproducttomcatwar;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class TestproducttomcatwarApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(TestproducttomcatwarApplication.class, args);
    }
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(TestproducttomcatwarApplication.class);
    }
}

在这段代码中,一定要继承父类并复写configure函数。

为什么要写configure函数?

是为了让导出的war包给tomcat使用的时候可以给tomcat识别到并调用

复制代码
TestproducttomcatwarApplication.class

依赖pom.xml如下:

复制代码
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>4.0.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>org.example</groupId>
    <artifactId>testproducttomcatwar</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <!-- 打包方式修改成 war -->
    <packaging>war</packaging>
    <name>testproducttomcatwar</name>
    <description>testproducttomcatwar</description>
    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

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


        <!-- Web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!-- 排除内置 Tomcat -->
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- 使用外部容器时提供 Tomcat 运行环境 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>

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

</project>

运行操作:

一、

idea运行

资源里的testproducttomcatwar项目,改项目终端运行mvn clean install,

生成war包

二、

将1中的war包粘贴到项目

运行tomcattest项目时需要配置,

配置如下:

添加后

在配置里修改url

运行后,得到效果:

。。

相关推荐
稳联技术老娜15 分钟前
伺服内嵌EtherNet IP方案:嵌入式小板SPI通讯固件Demo程序适配改造
运维·服务器·网络
爱学堂IT分享16 分钟前
Cesium可视化系统实战课程-Cesium教程学习
前端
糖墨夕40 分钟前
理解大语言模型:Agent 的“大脑”
前端·agent
Rain的Java大神之路1 小时前
JavaWeb开发如何解决跨域问题
java·前端·后端·nginx·web安全·面试·运维开发
en.en..1 小时前
Linux系统编程:fcntl 与 ioctl
linux·运维·服务器
自动化监测Learner1 小时前
Navicat Premium 17 中文版安装教程(2026最新版)
java·linux·数据库
cpolar技术支持1 小时前
浏览器也能跑本地 AI:用 Transformers.js + WebGPU 做一个最小推理 Demo,cpolar 给同事远程体验
前端·ai·cpolar·webgpu·transformers.js
那个松鼠很眼熟w1 小时前
4.Spring-Ai入门案例
java·人工智能·spring
计算机毕设定制辅导-无忧学长1 小时前
《基于SpringBoot青年公寓出租管理系统的设计与实现》
java·vue.js·spring boot·青年公寓出租管理系统
会周易的程序员2 小时前
5Draft(五帝)测试报告
服务器·c++·分布式·raft·共识算法·共识·算力服务器