news 2026/7/1 8:33:40

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

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
两个步骤,打包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

运行后,得到效果:

。。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/30 4:23:00

Flutter 应用保活与后台任务:在 OpenHarmony 上实现定时上报

前言 在 OpenHarmony 生态中&#xff0c;许多应用场景&#xff08;如健康监测、设备状态上报、位置追踪&#xff09;要求应用即使在退到后台或屏幕关闭后&#xff0c;仍能周期性执行任务。然而&#xff0c;出于系统资源与电池优化的考虑&#xff0c;OpenHarmony 对后台进程有严…

作者头像 李华
网站建设 2026/6/29 8:11:45

【RL】verl 数据处理

您的 Eurus-2-RL-Data 数据集需要做两个主要适配&#xff1a;文件格式转换和字段映射配置。 快速解决方案 1. 转换文件格式&#xff08;推荐&#xff09; 将 arrow 文件转换为 parquet 格式&#xff1a; from datasets import load_dataset import os# 加载原始数据 ds lo…

作者头像 李华
网站建设 2026/6/30 14:15:30

Product Hunt 每日热榜 | 2025-12-13

1. Gemini Deep Research Agent 标语&#xff1a;最优秀的研究助手现已向开发者开放&#xff01; 介绍&#xff1a;Gemini深度研究助手现在可以通过互动API提供给开发者使用。它由Gemini 3.0 Pro驱动&#xff0c;能够自主规划、执行和综合多步骤的研究任务。 产品网站&#…

作者头像 李华
网站建设 2026/7/1 12:46:19

Python内置函数:你以为你很熟,但这些用法90%的人不知道

你好&#xff0c;我是你的技术朋友。今天我想和你聊聊那些每天都在用&#xff0c;却可能只用了十分之一功能的Python内置函数。 想象一下&#xff0c;你家厨房有一套顶级厨刀&#xff0c;但平时只用它切切西红柿。直到有天看到大厨用同一把刀雕出一朵萝卜花&#xff0c;你才恍然…

作者头像 李华
网站建设 2026/6/25 22:43:22

python_基于主视频删减片段并插入镜头视频

python_基于主视频删减片段并插入镜头视频 import pyJianYingDraft as draft from pyJianYingDraft import trange, ClipSettings,timdef create_jianying_draft_from_clips(draft_name,main_video_path,delete_ranges,lens_info_dict,draft_folder_path):# 时间格式转换函数(处…

作者头像 李华