news 2026/6/15 17:13:49

两个步骤,打包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/15 6:13:12

中望CAD2026如何将图形移动到坐标原点

在中望CAD 2026中&#xff0c;将图形移动到坐标原点&#xff08;0,0,0&#xff09;主要有以下几种常用方法&#xff1a; 使用“移动”命令&#xff08;最常用&#xff09; 1.全选图形 按 Ctrl A 选择所有图形对象&#xff0c;或手动框选需要移动的图形。执行移动命令 输入 M&a…

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

Zig 语言实战:实现高性能快速排序算法

在上一篇博客中&#xff0c;我们深入探讨了如何在 Rust 中利用 Ord Trait 和 checked_sub 来实现一个安全的快速排序。今天&#xff0c;我们将视角转向 Zig 语言。 Zig 被设计为 C 语言的现代替代品&#xff0c;它没有隐藏的控制流&#xff0c;内存管理完全由开发者掌控。在实现…

作者头像 李华
网站建设 2026/6/8 20:16:36

用你的生日,取一个微信昵称

出生月份✨&#xff1a; 1.干饭的 2.摸鱼的 3.追剧的 4.炫奶茶的 5.爱睡的 6.摆烂的 7.发疯的 8.撸猫的 9.社恐的 10.暴富的 11.吃瓜的 12.划水的 出生日期&#x1f973;&#xff1a; 1.小土豆 2.糯叽叽 3.胖嘟嘟 4.碎碎冰 5.懒羊羊 6.皮卡丘 7.小趴菜 8.毛肚卷 9.酸辣粉 10.Q…

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

453453

5445345

作者头像 李华
网站建设 2026/6/15 15:19:37

初级菜鸟快速学习无人机电调教程:第4节

第四阶段&#xff1a;安装、焊接与基础设置&#xff08;第18-21天&#xff09;核心目标&#xff1a;将理论转化为实践&#xff0c;安全、规范地完成电调与电机、飞控、电池的物理连接与焊接&#xff0c;并完成上电前的基础设置&#xff0c;为首次通电测试做好准备。学习小节苏格…

作者头像 李华